如何使用选项和查询转义命令?

时间:2011-04-06 10:25:20

标签: bash unix scripting curl escaping

这是我在脚本中的方式。逃避它有什么不对?

"curl --fail $solrIndex/update?commit=true -H \"Content-Type: text/xml\" --data-binary '<delete><query>*:*</query></delete>'"

这就是它的执行方式:

curl --fail http://localhost:8080/solr/update?commit=true -H "Content-Type: text/xml" --data-binary '<delete><query>*:*</query></delete>'
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">17</int></lst>
</response>
curl: (6) Couldn't resolve host 'text'

什么有效:

$ curl --fail http://localhost:8080/solr/update?commit=true -H "Content-Type: text/xml" --data-binary '<delete><query>*:*</query></delete>'
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">51</int></lst>
</response>

2 个答案:

答案 0 :(得分:1)

将命令存储在数组中而不是单个字符串

cmd=(curl --fail $solrIndex/update?commit=true -H "Content-Type: text/xml" --data-binary '<delete><query>*:*</query></delete>')

执行
"${cmd[@]}"

答案 1 :(得分:0)

你几乎从不引用整个命令;引用这些论点:

curl --fail "$solrIndex/update?commit=true" -H "Content-Type: text/xml" --data-binary '<delete><query>*:*</query></delete>'