【发布时间】:2018-01-27 08:48:48
【问题描述】:
我正在尝试从 shell 脚本执行 curl 命令。此命令在 cli 中正常工作。但是,我需要在 shell 脚本中执行它。 curl 命令预计会根据查询删除 solr 上的记录。
curl -v -o /dev/null -w %{http_code} -H Content-Type:text/xml -X POST http://localhost:8983/solr/content/update?commit=true -d <delete><query>playlist_id:["" TO *]</query></delete>
Note: Unnecessary use of -X or --request, POST is already inferred.
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8983 (#0)
> POST /solr/content/update?commit=true HTTP/1.1
> Host: localhost:8983
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type:text/xml
> Content-Length: 30
>
} [30 bytes data]
* upload completely sent off: 30 out of 30 bytes
< HTTP/1.1 400 Bad Request
< Content-Type: application/xml; charset=UTF-8
< Content-Length: 488
<
{ [488 bytes data]
100 518 100 488 100 30 63649 3912 --:--:-- --:--:-- --:--:-- 69714
* Connection #0 to host localhost left intact
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: TO/
* Could not resolve host: TO
* Closing connection 1
curl: (6) Could not resolve host: TO
curl: (3) [globbing] unmatched close brace/bracket in column 2
400000
status: '400000'
Critical error happened during solr data delete: Verify the state manually
Quitting now
我需要检索 http 状态码来验证状态。执行以下 shell 脚本代码。
curl_command_tmp="http://localhost:8983/solr/content/update?commit=true -d <delete><query>playlist_id:["" TO *]</query></delete>"
curl_command_tmp="curl -v -o /dev/null -w %{http_code} -H Content-Type:text/xml -X POST $curl_command_tmp"
echo "Curl delete command $curl_command_tmp"
status=`$curl_command_tmp`
echo $status
echo "status: '$status'"
if [ "$status" != "200" ]; then
echo "Critical error happened during solr data delete: Verify the state manually"
echo "Quitting now"
exit 0
fi
我尝试了不同的 curl 选项(例如 -G 、--data-urlencode)。但他们不工作。我想如果我可以通过完整的请求而不被截断,那么它会起作用。但它在 solr http 请求正文中的“TO”之前被截断。 我已经浏览了各种论坛,但尚未解决。需要您的帮助来解决它
【问题讨论】:
-
这里的双引号
["" TO *]混淆了脚本外壳。它认为第一个"与命令开头的"匹配,第二个""开始一个与命令末尾的"匹配的新字符串。结果是""消失了。尝试使用反斜杠重写该表达式,以保护每个双引号不被视为字符串分隔符,例如[\"\" TO *]。