简体   繁体   English

URL以可移植的方式在Shell脚本中编码字符串

[英]URL encoding a string in shell script in a portable way

I need to URL encode a string using a shell function that will run in BusyBox Ash, Dash, Bash and ZSH. 我需要使用将在BusyBox Ash,Dash,Bash和ZSH中运行的Shell函数对字符串进行URL编码。

It will run in different Docker containers, so it'd be good to have as little dependencies to install as possible. 它将在不同的Docker容器中运行,因此最好安装尽可能少的依赖项。

Notes: 笔记:

  • The question " URL encoding a string in bash script " asks for a Bash-specific script, and the only provided answer depends on PHP being installed on the container. 问题“ URL在bash脚本中编码字符串 ”要求使用Bash特定的脚本,唯一提供的答案取决于容器上安装的PHP。

  • The question " How to urlencode data for curl command? " is specific to curl , but is certainly open to non-specific answers. 问题“ 如何为curl命令使用urlencode数据? ”这个问题专门针对curl ,但是肯定会涉及到非特定的答案。 However, none of the 25 answers seem to apply. 但是,这25个答案似乎都不适用。 One of the answers there only works to send data to curl , while some are specific to bash or ksh , some others require Perl , PHP , Python , Lua , NodeJS , Ruby , gridsite-clients , uni2ascii , jq , awk or sed to be installed. 其中一个答案那里只能将数据发送到卷曲 ,而有些是特定于bashksh的 ,有些则需要Perl,PHP,PythonLua的NodeJS, 红宝石gridsite的客户端 ,uni2ascii,JQ,AWKsed的是已安装。 One of them doesn't require additional dependencies, but doesn't preserve characters like a , 1 and ~ . 其中一个不需要额外的依赖关系,但不保留像, 1~这样a字符。

What I'd expect to have: 我希望有:

$> urlencode '/'
%2f

$> urlencode 'ç'
%c3%a7

$> urlencode '*'
%2a

$> urlencode abc-123~6
abc-123~6

$> urlencode 'a test ?*ç '
a%20test%20%3f%2a%c3%a7%20

The functions below have been tested in BusyBox Ash , Dash , Bash and ZSH . 以下功能已在BusyBox AshDashBashZSH中进行了测试。

They only use shell builtins or core commands and should work in other shells as well. 它们仅使用Shell内置函数或核心命令,并且也应在其他Shell中工作。

urlencodepipe() {
  local LANG=C; local c; while IFS= read -r c; do
    case $c in [a-zA-Z0-9.~_-]) printf "$c"; continue ;; esac
    printf "$c" | od -An -tx1 | tr ' ' % | tr -d '\n'
  done <<EOF
$(fold -w1)
EOF
  echo
}

urlencode() { printf "$*" | urlencodepipe ;}

How it works: 这个怎么运作:

  • Standard input is processed by fold -w1 , which re-formats its input so that it is 1 column wide (in other words, it adds a \\n after each input character so that each character will be on its own line) 标准输入由fold -w1处理,它重新格式化其输入以使其为1列宽(换句话说,它在每个输入字符后添加一个\\n ,以便每个字符都在其行上)
  • The here-document <<EOF feeds the output of fold to the read command 此处的文档<<EOFfold的输出提供给read命令
  • The while command accepts one line at a time (which is only 1 character wide) from the read command, which gets its input from fold , and assigns it to variable c while命令一次从read命令接受一行(仅1个字符宽),该命令从fold获取其输入,并将其分配给变量c
  • case tests if that character needs to be url encoded. case测试该字符是否需要进行网址编码。 If it doesn't, then it's printed and the while loop continues 如果没有,则将其打印出来,并继续while循环
  • od converts each input character to hex od将每个输入字符转换为十六进制
  • tr converts spaces to % and joins multiple lines tr将空格转换为%并连接多行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM