출처 : http://it.toolbox.com/wiki/index.php/Uri_escape_bash_shell_function

#@ uri_escape - convert STRING to hex-encoded string
#@              and assign to optional VAR or _URI_ESCAPE
#@ Author: Chris F.A. Johnson, 2009-03-25
##

uri_escape() #@ USAGE: uri_escape STRING [VAR]
{
  local string x
  string=$1
  var=${2:-_URI_ESCAPE}

  while [ -n "$string" ] ## loop until $string is empty
  do

     case $string in

       ## If alphanumeric or underscore, add it unchanged
       [_a-zA-Z0-9]* ) printf -v x "%s%c" "$x" "$string" ;;

       ## ...otherwise convert first character to hexadecimal
       *) printf -v x "%s%%%X" "$x" "'$string" ;;

     esac

     string=${string#?} ## remove first character from string
  done

  eval "$var=\$x"
}


uri_escape "test - !test" encoded
echo "$encoded"

저작자 표시 비영리 동일 조건 변경 허락
Posted by 파이델