curl --fail 不抑制标准输出

curl --fail without suppressing stdout

我有一个使用 curl 将文件上传到 WebDav 服务器的脚本。

curl --anyauth --user user:password file http://webdav-server/destination/

我同时想要两件事:

据我所知,即使在 401(未授权)或 407(冲突)情况下,curl returns 退出代码 0 也是如此。 --fail 选项可用于更改此行为,但它会抑制标准输出。

最好的解决方法是什么? teegrep?

curl 将其输出写入 stderr(2),错误流而不是 stdout(1)。使用 2>&1

在命令上重定向它
curl --fail --anyauth --user user:password file http://webdav-server/destination/ 2>&1 > logFile
retval=$?
if [ $retval -eq 0 ]; then
    echo "curl command succeeded and the log present in logFile"
fi