简体   繁体   English

如何将包含引号和空格的变量作为单个参数的一部分传递给命令

[英]How to pass a variable containing quotes and spaces as part of a single argument to a command

I have the following line in my bash script: 我的bash脚本中有以下行:

curl -i -X POST -H 'Content-Type: application/json-rpc' \
     -d '{"jsonrpc": "2.0", "method": "configuration.export", "params": { "options": { "templates": [ '$ftemplateids' ] }, "format": "xml"}, "auth": '$authtoken', "id": 1 }' \
     http://example.com/zabbix/api_jsonrpc.php |
grep jsonrpc | tail -c +28 | head -c -12 > file.xml

As you can see, there are two variables: $authtoken and $ftemplateids . 如您所见,有两个变量: $authtoken$ftemplateids I have no problems with $authtoken ; 我没有$authtoken问题; it gets parsed without any problems. 它被解析没有任何问题。 The problem is with $ftemplateids . 问题出在$ftemplateids

When I echo the variable, it shows as this, which is the desired format: 当我echo该变量时,它显示如下,这是所需的格式:

"10001", "10047", "10050", "10056", "10060", "10065", "10066", "10067", "10068", "10069", "10070", "10071", "10072", "10073", ......., "14433"

However, when I run the script, it outputs the following: 但是,当我运行脚本时,它输出以下内容:

curl -i -X POST -H 'Content-Type: application/json-rpc' -d '{"jsonrpc": "2.0", "method": "configuration.export", "params": { "options": { "templates": [ "10001",' '"10047",' '"10050",' '"10056",' '"10060",' '"10065",' '"10066",' '"10067",' '"10068",' '"10069",' '"10070",' '"10071",' '"10072",' '"10073",' '"10074",' '"10075",' '"10076",' '"10077",' '"10078",' '"10079",' '"10081",' '"10082",' '"10083",' '"10086",' '"10089",' '"10592",' '"10595",' '"10599",' '"10726",' '"10737",' '"10739",' '"10758",' '"10763",' '"10764",' '"10769",' '"10776",' '"10809",' '"10810",' '"10876",' '"10880",' '"10881",' '"10882",' '"10921",' '"10924",' '"10961",' '"10966",' '"11005",' '"11006",' '"11007",' '"11008",' '"11010",' '"11011",' '"11012",' '"11035",' '"11036",' '"11037",' '"11038",' '"11120",' '"11126",' '"11146",' '"11147",' '"11148",' '"11149",' '"11151",' '"11167",' '"11168",' '"11169",' '"11170",' '"11264",' '"12256",' '"12264",' '"12281",' '"12315",' '"12418",' '"12420",' '"12780",' '"12788",' '"12799",' '"13771",' '"13772",' '"13776",' '"13781",' '"13790",' '"13791",' '"14433" ] }, "format": "xml"}, "auth": "authtokenwashere", "id": 1 }' http://example.com/zabbix/api_jsonrpc.php

I've tried [ "$ftemplateids" ] but that did not help. 我已经尝试过[ "$ftemplateids" ]但这没有帮助。

Your instinct to use double quotes is correct, but you still need to terminate the single quoted string. 您使用双引号的本能是正确的,但是您仍然需要终止单引号的字符串。 Try: 尝试:

… [ '"$ftemplateids"' ] …

William Pursell gives the right way to solve your problem. William Pursell提供了解决问题的正确方法。

Some explanation of why this happens: 为什么会发生这种情况的一些解释:

Because "The shell scans the results of parameter expansion , command substitution, and arithmetic expansion that did not occur within double quotes for word splitting ." 因为“ shell扫描了参数扩展 ,命令替换和算术扩展的结果,这些结果在双引号中没有出现,以进行词拆分 。”

The shell treats each character of $IFS as a delimiter, and splits the results of the other expansions into words on these characters. Shell将$IFS每个字符视为定界符,并将其他扩展的结果拆分为这些字符上的单词。 If IFS is unset, it is <space><tab><newline> by default 如果未设置IFS,则默认为<space><tab><newline>

In your command $ftemplateids was not enclosed with double quotes, and you have spaces in your variable, so your variable was split into multiple arguments. 在命令中$ftemplateids没有用双引号引起来,并且变量中有空格,因此变量被拆分为多个参数。

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

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