将带有空格的变量扩展为curl POST 变量
一些研究揭示了一些有用的 stackexchange 帖子,即 在 CURL 中扩展变量,但给出的答案并不似乎无法正确处理其中包含空格的 bash 变量。
我正在为 awk 的输出设置一个变量,解析字符串中的子字符串(实际上截断为 150 个字符)。我尝试通过curl POST 的字符串中有空格。
当我使用以下curl参数时,POST变量Body
被设置为第一个空格之前的字符串部分。
卷曲 -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
smsbody
设置为:
smsbody="$(echo $HOSTNAME$ $SERVICEDESC$ in $SERVICESTATE$\: $SERVICEOUTPUT$ | awk '{print substr($0,0,150)}')"
因此 smsbody
中唯一被 POST 的部分是 $HOSTNAME$
(恰好是一个没有任何空格字符的字符串)。
我应该使用什么curl 语法来正确嵌套bash 变量以进行扩展,但将其视为单个数据字段?
看起来很微不足道,但我把引号弄乱了一段时间,但运气不佳。我认为具有更好 CLI 功能的人可以在一秒钟内处理它。
谢谢!
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.
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.
When I use the following curl arguments, the POST variable Body
is set to the part of the string before the first space.
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
smsbody
is set as:
smsbody="$(echo $HOSTNAME$ $SERVICEDESC$ in $SERVICESTATE$\: $SERVICEOUTPUT$ | awk '{print substr($0,0,150)}')"
So the only portion of smsbody
that is POSTed is $HOSTNAME$
(which happens to be a string without any space characters).
What is the curl syntax I should use to nest the bash variable properly to expand, but be taken as a single data field?
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.
Thanks!
看起来您在 Body 之前有一个额外的单引号。您还需要双引号,否则 $smsbody 将不会被评估。
试试这个:
如果
$
仍然是一个问题(我不认为空格是),请尝试在它们前面添加一个\
:如果我运行
nc -l 5000
并将 twilio 地址更改为 localhost:5000,我看到 smsbody 变量正确输入。It looks like you have an extra single quote before Body. You also need double quotes or the $smsbody won't be evaluated.
Try this:
If the
$
s are still an issue (I don't think spaces are), try this to prepend a\
to them:If I run
nc -l 5000
and change the twilio address to localhost:5000, I see the smsbody variable coming in properly.