bash 将带有空格的变量扩展为 curl POST 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7637078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:54:52  来源:igfitidea点击:

Expanding variable with spaces into a curl POST variable

bashpostcurlnagios

提问by mbrownnyc

Some research revealed a few useful stackexchange posts, namely expanding variable in CURL, but that given answer doesn't seem to properly handle bash variables that have spaces in them.

一些研究揭示了一些有用的 stackexchange 帖子,即在 CURL 中扩展变量,但给出的答案似乎没有正确处理其中有空格的 bash 变量。

I am setting a variable to the output of awk, parsing a string for a substring (actually truncating to 150 characters). The string I am attempting to POST via curl has spaces in it.

我正在为 awk 的输出设置一个变量,解析子字符串的字符串(实际上截断为 150 个字符)。我试图通过 curl POST 的字符串中有空格。

When I use the following curl arguments, the POST variable Bodyis set to the part of the string before the first space.

当我使用以下 curl 参数时,POST 变量Body设置为第一个空格之前的字符串部分。

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/GUID/SMS/Messages.xml' -d 'From=DIDfrom' -d 'To=DIDto' -d 'Body="'$smsbody'" -u SECGUID

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/GUID/SMS/Messages.xml' -d 'From=DIDfrom' -d 'To=DIDto' -d 'Body="'$smsbody'" -u SECGUID

smsbodyis set as:

smsbody设置为:

smsbody="$(echo $HOSTNAME$ $SERVICEDESC$ in $SERVICESTATE$\: $SERVICEOUTPUT$ | awk '{print substr($0,0,150)}')"

smsbody="$(echo $HOSTNAME$ $SERVICEDESC$ in $SERVICESTATE$\: $SERVICEOUTPUT$ | awk '{print substr($0,0,150)}')"

So the only portion of smsbodythat is POSTed is $HOSTNAME$(which happens to be a string without any space characters).

所以smsbodyPOSTed的唯一部分是$HOSTNAME$(恰好是一个没有任何空格字符的字符串)。

What is the curl syntax I should use to nest the bash variable properly to expand, but be taken as a single data field?

我应该使用什么 curl 语法来正确嵌套 bash 变量以展开,但被视为单个数据字段?

Seems pretty trivial, but I messed with quotes for a while without luck. I figure someone with better CLI-fu can handle it in a second.

看起来很微不足道,但我在没有运气的情况下弄乱了一段时间的引号。我认为有更好的 CLI-fu 的人可以在一秒钟内处理它。

Thanks!

谢谢!

采纳答案by Matt Williamson

It looks like you have an extra single quote before Body. You also need double quotes or the $smsbody won't be evaluated.

看起来您在Body之前有一个额外的单引号。您还需要双引号,否则将不会评估 $smsbody。

Try this:

尝试这个:

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/GUID/SMS/Messages.xml' \
    -d 'From=DIDfrom' -d 'To=DIDto' -d "Body=$smsbody" -u SECGUID

If the $s are still an issue (I don't think spaces are), try this to prepend a \to them:

如果$s 仍然是一个问题(我不认为空格是),请尝试\在它们前面加上 a :

smsbody2=`echo $smsbody | sed 's/\$/\\$/g'`
curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/GUID/SMS/Messages.xml' \
    -d 'From=DIDfrom' -d 'To=DIDto' -d "Body=$smsbody2" -u SECGUID

If I run nc -l 5000and change the twilio address to localhost:5000, I see the smsbody variable coming in properly.

如果我运行nc -l 5000并将 twilio 地址更改为 localhost:5000,我会看到 smsbody 变量正确输入。

matt@goliath:~$ nc -l 5000POST / HTTP/1.1
Authorization: Basic U0VDR1VJRDphc2Q=
User-Agent: curl/7.21.6 (x86_64-apple-darwin10.7.0) libcurl/7.21.6 OpenSSL/1.0.0e zlib/1.2.5 libidn/1.20
Host: localhost:5000
Accept: */*
Content-Length: 45
Content-Type: application/x-www-form-urlencoded

From=DIDfrom&To=DIDto&Body=goliath$ $ in $: