diff --git a/lib/sharedFunctions.sh b/lib/sharedFunctions.sh index d421611..265cbbf 100755 --- a/lib/sharedFunctions.sh +++ b/lib/sharedFunctions.sh @@ -46,7 +46,9 @@ function readFile() { # Escape a string # ------------------------------------------------------ -escape() { echo $@ | sed 's/\//\\\//g'; } +# usage: var=$(escape "String") +# ------------------------------------------------------ +escape() { echo $@ | sed 's/[]\.|$(){}?+*^]/\\&/g'; } # needSudo # ------------------------------------------------------ @@ -510,4 +512,25 @@ progressBar() { fi tput cnorm +} + +# URL encoding/decoding from: https://gist.github.com/cdown/1163649 +urlencode() { + # urlencode + + local length="${#1}" + for (( i = 0; i < length; i++ )); do + local c="${1:i:1}" + case $c in + [a-zA-Z0-9.~_-]) printf "$c" ;; + *) printf '%%%02X' "'$c" + esac + done +} + +urldecode() { + # urldecode + + local url_encoded="${1//+/ }" + printf '%b' "${url_encoded//%/\x}" } \ No newline at end of file