mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-09 21:53:47 -05:00
functions for encoding/decoding URLS
This commit is contained in:
@@ -46,7 +46,9 @@ function readFile() {
|
|||||||
|
|
||||||
# Escape a string
|
# Escape a string
|
||||||
# ------------------------------------------------------
|
# ------------------------------------------------------
|
||||||
escape() { echo $@ | sed 's/\//\\\//g'; }
|
# usage: var=$(escape "String")
|
||||||
|
# ------------------------------------------------------
|
||||||
|
escape() { echo $@ | sed 's/[]\.|$(){}?+*^]/\\&/g'; }
|
||||||
|
|
||||||
# needSudo
|
# needSudo
|
||||||
# ------------------------------------------------------
|
# ------------------------------------------------------
|
||||||
@@ -511,3 +513,24 @@ progressBar() {
|
|||||||
|
|
||||||
tput cnorm
|
tput cnorm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# URL encoding/decoding from: https://gist.github.com/cdown/1163649
|
||||||
|
urlencode() {
|
||||||
|
# urlencode <string>
|
||||||
|
|
||||||
|
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 <string>
|
||||||
|
|
||||||
|
local url_encoded="${1//+/ }"
|
||||||
|
printf '%b' "${url_encoded//%/\x}"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user