当curl获得非200 HTTP状态时退出bash脚本


11

我有一个bash脚本设置来执行一些curl请求

for currency in EUR INR JPY
do
  curl -i --data '{"currency": "'$currency'"}' -H "Accept: application/json" -H "Content-Type: application/json" http://0.0.0.0:8080/price && echo
done

如果其中一个curl响应返回http状态!= 200,是否有办法使脚本退出?

我还希望保留标准的curl输出,例如,我不希望仅打印http状态代码的解决方案。

干杯

Answers:


5

--failman页面中所述,似乎可以完成此工作:

$ curl --fail --location http://google.com/nope
$ echo $?
22

4
从联机帮助页来看,--fail由于以下两个原因而不合适:1 Fail silently (no output at all).和2 This method is not fail-safe and there are occasions where non-successful response codes will slip through
Armand
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.