urldeconstruct/urldeconstruct

67 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# May cause fork bomb, do not use this!
#url="$1"
# Set a user limit just to be safe.
# Current process count seems to be between 512 and 600.
# Quite unsettling...
ulimit -u 600
# And query interactively from user instead.
read -sp "URL: " url
#printf "%s\n\n" "$url"
printf "\n"
# Prepare URL
url=$(sed 's/\?/\n/g' <<< $url)
url=$(sed 's/\&/\n/g' <<< $url)
# Turn it into an array
url_array=$(sed 's/\n/ /g' <<< $url)
url_array=($url_array)
#printf "%s\n" "${url_array[*]}"
#printf "%s %s\n" "Base URL:" "${url_array[0]}"
# https://stackoverflow.com/questions/6723426
for url_argument in "${!url_array[@]}"
do
# Zero is base URL, everything else is arguments
if [ "$url_argument" = "0" ]
then
export url_property="Base URL:"
elif [ "$url_argument" != "0" ]
then
export url_property="Argument:"
fi
# Separate key=value pairs into an array
url_parameter=($(sed 's/\=/ /g' <<< ${url_array[$url_argument]}))
# Differentiate trackers from other arguments
if grep -q utm <<< ${url_parameter[0]}
then
url_property="Tracker:"
fi
# Turn %20 / + urlencoding into human-readable spaces
url_despace=${url_parameter[1]}
[ $(grep '%20' <<< "$url_despace") ] && \
url_despaced="$(sed 's/\%20/ /g' <<< $url_despace)"
[ $(grep '+' <<< "$url_despace") ] && \
url_despaced="$(sed 's/\+/ /g' <<< $url_despace)"
# For publication purposes only
[ ${url_parameter[0]} = "ea.url.id" ] && \
url_parameter[1]="redacted"
# Print out the resulting text
printf "%-10s%-15s%-15s\n" "$url_property" "${url_parameter[0]}" "${url_parameter[1]}" #"${url_array[$url_argument]}"
[ ! -z "$url_despaced" ] &&
printf "%-25s%15s\n" "" "$url_despaced"
# Clear variables for next loop
unset url_despaced
done