我想运行一个命令,然后检查字符串“200 OK”的结果。如何在bash脚本中解析命令的结果
到目前为止,我有以下bash脚本:
for i in $(seq 1 50)
do
printf "i is: $i\n"
RESULTS="$(curl -i -H "Accept: application/json" -X GET http://localhost/test/store/widget/123 | grep -ne "200 OK")"
if [ $RESULTS -eq 0 ]; then
echo "GET failed with $RESULTS"
break
else
echo "we're good"
fi
done
当我运行上面的脚本,我得到如下结果:
i is: 1
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 85 100 85 0 0 3944 0 --:--:-- --:--:-- --:--:-- 4047
gettest.sh: 8: [: 1:HTTP/1.1: unexpected operator
we're good
i is: 2
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 85 100 85 0 0 3293 0 --:--:-- --:--:-- --:--:-- 3400
gettest.sh: 8: [: 1:HTTP/1.1: unexpected operator
we're good
的输出,我回来从运行curl命令一次看起来像这样:
HTTP/1.1 200 OK
Date: Tue, 10 Feb 2015 13:21:18 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.5
Content-Length: 85
Content-Type: application/json; charset=utf-8
{"status":pass,"widgetinfo":"{\"widgetname\":\"the best widget\",\"price\":\"100.00\"}"}
基于上述结果,我有几个问题s:
- 为什么bash脚本会打印有关总时间,速度等的统计数据以获得curl结果?
- 我该如何修改脚本,以便除非脚本失败,我只想看到计数器值(i)和指示通过的状态。如果失败,我希望脚本退出,并详细说明curl命令返回的内容。
- 为什么我收到错误
gettest.sh:8:1:HTTP/1.1:意外的运营商?
任何提示?谢谢!
好的技巧得到HTTP_CODE用'-w' +1 – Tiago 2015-02-10 23:10:52