curl shell 命令始终返回错误状态 0 - 即使输入错误失败

curl shell 命令始终返回错误状态 0 - 即使输入错误失败

例如:尝试上传一个不存在的文件--curl -d @no-real-file http://google.com | echo $?返回0。

如何让 curl 因输入错误而失败?

答案1

您可以使用curl -f

-f, --fail  (HTTP) Fail silently (no output at all) on server errors. 
This is mostly done to better enable scripts etc to better deal with failed attempts. 
In normal cases when a HTTP server  fails  to  deliver  a  document,  it returns 
an HTML document stating so (which often also describes why and more). This flag 
will prevent curl from outputting that and return error 22.

This method is not fail-safe and there are occasions where non-successful response 
codes will slip through, especially when authentication is involved (response codes 
401 and 407).

如果-f使用,curl将返回22以下命令

curl -f -d @no-real-file http://google.com

答案2

这可能不是您想要的,但您可以简单地快速检查一下该文件是否存在:

[ -f $somefile ] && curl -d $somefile blah blah

相关内容