本文介绍了管道命令的输出发球又节约命令退出code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个shell脚本中,我换一个命令(MVN全新安装),将输出到日志文件重定向。

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

现在,如果 MVN全新安装失败,出现错误,我想我的包装的shell脚本也会失败与错误。但是因为我管道全部输出到三通,我无法访问 MVN返回code干净安装,所以当我访问 $ ?之后,它始终为0(因为发球成功)。

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).

我试图让命令写入错误输出到一个单独的文件,并检查之后,但MVN的错误输出始终是空的(好像它只是写入stdout)。

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).

我如何preserve MVN返回code干净的安装但还是输出管道到一个日志文件?

How can I preserve the return code of mvn clean install but still piping the output to a logfile?

推荐答案

既然你正在运行庆典,您可以使用它的变量代替 $

Since you're running bash, you can use its $PIPESTATUS variable instead of $?:

mvn clean install $@ | tee $logfile
echo ${PIPESTATUS[0]}

这篇关于管道命令的输出发球又节约命令退出code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 06:18