开发者

How do I escape a cmd with options and a query?

开发者 https://www.devze.com 2023-02-22 16:18 出处:网络
This is how I have it in the script. What\'s wrong escaping it? \"curl --fail $solrIndex/update?commit=true -H \\\"Content-Type: text/xml\\\" --data-binary \'<delete><query>*:*</query&

This is how I have it in the script. What's wrong escaping it?

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

This is how it executes:

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'

What works:

$ 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"?>
<res开发者_如何学Cponse>
<lst name="responseHeader"><int name="status">0</int><int name="QTime">51</int></lst>
</response>


Store the command in an array instead of a single string

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

And execute it with

"${cmd[@]}"


You should almost never quote the whole command; quote the arguments:

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

精彩评论

暂无评论...
验证码 换一张
取 消