출처 : 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"
}
#@ 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"
echo "$encoded"
'쉘스크립트 > 유용한함수' 카테고리의 다른 글
| urldecode functions (0) | 2009/11/23 |
|---|---|
| urlencode - convert STRING to hex-encoded string (0) | 2009/11/23 |
| 숫자 목록의 총 합계 구하기 (더하기) - sh (1) | 2009/11/17 |
| input 다이얼로그 만들기 - sh (0) | 2009/11/17 |