URL编码bash脚本字符串 [英] URL encoding a string in bash script

查看:182
本文介绍了URL编码bash脚本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在我试图提交后的变量bash脚本,但是wget的是把它当作多个URL,我相信,因为它不是URLEN codeD盒... ...这是我的基本思路。

I am writing a bash script in where I am trying to submit a post variable, however wget is treating it as multiple URLS I believe because it is not URLENCODED... here is my basic thought

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没有获得这个职位变数,再加上它pretty玉米粥是说

I am getting errors and the alert.php is not getting the post variable plus it pretty mush is saying

解决不了我
无法解析上午
无法解析尝试
..等等。

can't resolve I can't resolve am can't resolve trying .. and so on.

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

My example above is a simple kinda sudo example but I believe if I can url encode it, it would pass, I even tried php like:

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

但PHP错误了..任何想法?我如何可以传递$消息中的变量,而不执行PHP呢?

but php errors out.. any ideas? How can i pass the variable in $MESSAGE without php executing it?

推荐答案

您想 $消息是在双引号,所以壳不能将其拆分成单独的词:

You want $MESSAGE to be in double-quotes, so the shell won't split it into separate words:

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

这篇关于URL编码bash脚本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆