curl --失败而不抑制标准输出

问题描述 投票:0回答:2

我有一个使用

curl
将文件上传到 WebDav 服务器的脚本。

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

我同时想要两件事:

  • 将脚本输出到标准输出(定向到日志文件)
  • 检测是否上传成功

据我所知,即使在 401(未经授权)或 407(冲突)情况下,curl 也会返回退出代码

0
--fail
选项可用于更改此行为,但它会抑制标准输出。

对此最好的解决方法是什么?

tee
grep

bash http curl webdav
2个回答
4
投票

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

0
投票

如果您将来发现这个问题,您现在可以使用新的fail-with-body标志

© www.soinside.com 2019 - 2024. All rights reserved.