URL编码在bash脚本的字符串(URL encoding a string in bash scr

2019-06-26 21:24发布

我写在我试图提交后可变bash脚本,但是wget的是把它当作多个网址,我相信,因为它不是url编码...这里是我的基本思路

MESSAGE='I am trying to post this information'
wget -O test.txt http://xxxxxxxxx.com/alert.php --post-data 'key=xxxx&message='$MESSAGE''

我得到的错误和alert.php没有获得这个职位变量再加上它漂亮的玉米粥是说

解决不了我无法解决时无法解决尝试..等等。

我上面的例子是一个简单的有点sudo的例子,但我相信,如果我能URL编码,它会通过,我甚至试过PHP这样的:

MESSAGE='I am trying to post this information'
MESSAGE=$(php -r 'echo urlencode("'$MESSAGE'");')

但PHP错误了..任何想法? 我怎么能不通过PHP执行它在$消息的变量?

Answer 1:

你想$MESSAGE是在双引号,所以shell将不会将其分割成单独的词:

ENCODEDMESSAGE=$(php -r "echo urlencode(\"$MESSAGE\");")


Answer 2:

在CentOS,没有多余的包装需要:

python -c "import urllib;print urllib.quote(raw_input())" <<< "$message"


Answer 3:

扩展Rockallite的非常有用的答案为Python 3和多输入从一个文件(这个时候在Ubuntu,但是这不应该的问题):

cat any.txt | python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.stdin.read()))"


Answer 4:

纯击方式:

URL='rom%C3%A2ntico'
echo -e "${URL//%/\\x}"

呼应:

romântico

“C3 A2”在UTF8十六进制“A”

UTF8名单: http://www.utf8-chartable.de/



文章来源: URL encoding a string in bash script