diff --git a/profile.d/net.sh b/profile.d/net.sh index cb12af7..2936b2a 100644 --- a/profile.d/net.sh +++ b/profile.d/net.sh @@ -41,6 +41,7 @@ isipv4() { # Set up local variables local ip=$1 + [[ -z $ip ]] && return 1 # Start with a regex format test if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then @@ -83,5 +84,24 @@ isipv6() } export -f isipv6 +# ------------------------------------------------------------------------------ +# Encode a string so it can be used as a URL parameter +# ------------------------------------------------------------------------------ +urlencode() { + local LANG=C + local str="$*" + local length="${#str}" + for (( i = 0; i < length; i++ )); do + local c="${str:i:1}" + case "$c" in + [a-zA-Z0-9.~_-]) printf "$c" ;; + ' ') printf '+' ;; + *) printf '%%%02X' "'$c" #| cut -d' ' -f2 ;; + esac + done +} +export -f urlencode + + # ------------------------------------------------------------------------------ # EOF