将命令输出传送到 tee,同时保存命令的退出代码
我有一个 shell 脚本,其中包含一个命令(mvn clean install),以将输出重定向到日志文件。
#!/bin/bash
...
mvn clean install $@ | tee $logfile
echo $? # Does not show the return code of mvn clean install
现在,如果 mvn clean install
因错误而失败,我希望我的包装器 shell 脚本也因该错误而失败。但由于我将所有输出通过管道传输到 tee,因此我无法访问 mvn clean install 的返回码,因此当我之后访问 $? 时,它始终为 0(因为发球成功)。
我尝试让命令将错误输出写入单独的文件并随后检查,但 mvn 的错误输出始终为空(似乎它只写入标准输出)。
如何保留 mvn clean install
的返回代码,但仍将输出通过管道传输到日志文件?
I have a shell script in which I wrap a command (mvn clean install), to redirect the output to a logfile.
#!/bin/bash
...
mvn clean install $@ | tee $logfile
echo $? # Does not show the return code of mvn clean install
Now if mvn clean install
fails with an error, I want my wrapper shell script also fail with that error. But since I'm piping all the output to tee, I cannot access the return code of mvn clean install
, so when I access $?
afterwards, it's always 0 (since tee successes).
I tried letting the command write the error output to a separate file and checking that afterwards, but the error output of mvn is always empty (seems like it only writes to stdout).
How can I preserve the return code of mvn clean install
but still piping the output to a logfile?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您正在运行
bash
,因此您可以使用其 $ PIPESTATUS 变量而不是$?
:Since you're running
bash
, you can use its $PIPESTATUS variable instead of$?
:您可以运行 mvn 命令并缓存退出代码...我在示例中使用“false”命令。
这样您就可以使用状态文件内容做出进一步的决策。
我现在很好奇是否有更雄辩的方法来实现这一点。
You could run the mvn command and cache the exit code... I use the "false" command for my example.
That way you can use the status file content to make further decisions.
I'm curious now whether there is a more eloquent way to accomplish this.
解决方法(注意:最好是@Frederic 的解决方案):
Workaround (note: a perfer @Frederic's solution):
您可以设置
pipefail
shell option 选项以获得您想要的行为。来自 Bash 参考手册:
示例:
要恢复原始管道设置:
You can set the
pipefail
shell option option on to get the behavior you want.From the Bash Reference Manual:
Example:
To restore the original pipe setting: