url_encode and url_decode in Shell

之前写过一版 shell下解码url,下面给出另外一个版本

from https://gist.github.com/cdown/1163649

function urlencode() {
    local LANG=C
    for ((i=0;i<${#1};i++)); do
        if [[ ${1:$i:1} =~ ^[a-zA-Z0-9\.\~\_\-]$ ]]; then
            printf "${1:$i:1}"
        else
            printf '%%%02X' "'${1:$i:1}"
        fi
    done
}
urldecode() {
    # urldecode <string>

    local url_encoded="${1//+/ }"
    printf '%b' "${url_encoded//%/\\x}"
}

好用就行

posted @ 2017-06-18 20:00  westfly  阅读(605)  评论(0编辑  收藏  举报