Shell脚本:如果用户错误,则获取cURL错误

时间:2018-11-20 12:38:24

标签: shell curl error-handling

我的shell脚本如下:

#!/bin/bash
curl -f -T /home/skript_1.txt -u XXX:XXXXXXX! -k http://192.168.0.100/home/test.txt
res=$?
if test "$res" != 0; then
   echo "the curl command failed with: $res"
else
   echo "Success $res"
fi

我用它来压缩文件...

现在我的问题是,我无法获得所有错误。 例如,如果我输入了错误的URL(正确的URL为http://192.168.0.100:5005/home/test.txt),则上传失败,但退出代码仍为0。

以下是网址错误的输出:

<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
Success 0

我也如何获得这些错误?

我也尝试过使用cURL和ftp目标进行相同的操作,在那里它可以处理所有错误。

1 个答案:

答案 0 :(得分:1)

Math.Tanh将使-w 'http_code %{http_code}'在输出末尾添加HTTP状态代码。

也许您可以使用这个新版本,但我仅对其进行了部分测试:

curl