我想在Bash中执行一个长时间运行的命令,并捕获它的退出状态,并输出它的输出。
所以我这样做:
command | tee out.txt
ST=$?
问题是变量ST捕获tee的退出状态,而不是命令的退出状态。我怎么解决这个问题?
注意,该命令长时间运行,将输出重定向到文件以供以后查看,这对我来说不是一个好的解决方案。
我想在Bash中执行一个长时间运行的命令,并捕获它的退出状态,并输出它的输出。
所以我这样做:
command | tee out.txt
ST=$?
问题是变量ST捕获tee的退出状态,而不是命令的退出状态。我怎么解决这个问题?
注意,该命令长时间运行,将输出重定向到文件以供以后查看,这对我来说不是一个好的解决方案。
当前回答
在普通bash中最简单的方法是使用进程替换而不是管道。有几个不同之处,但它们可能对你的用例不太重要:
当运行管道时,bash等待所有进程完成。 向bash发送Ctrl-C使其杀死管道的所有进程,而不仅仅是主要进程。 pipefail选项和PIPESTATUS变量与进程替换无关。 可能更
使用进程替换,bash只是启动进程,然后忘记它,它甚至在作业中都不可见。
除了上面提到的差异,消费者< <(生产者)和生产者|消费者本质上是相同的。
如果想要转换哪个进程是“主”进程,只需将命令和替换方向转换为生产者> >(消费者)。在你的情况下:
command > >(tee out.txt)
例子:
$ { echo "hello world"; false; } > >(tee out.txt)
hello world
$ echo $?
1
$ cat out.txt
hello world
$ echo "hello world" > >(tee out.txt)
hello world
$ echo $?
0
$ cat out.txt
hello world
正如我所说,它与管道表达式有不同之处。进程可能永远不会停止运行,除非它对管道关闭很敏感。特别是,它可能会不断向您的标准输出写入内容,这可能会令人困惑。
其他回答
通过结合PIPESTATUS[0]和在子shell中执行exit命令的结果,您可以直接访问初始命令的返回值:
命令| tee;退出${PIPESTATUS[0]}
这里有一个例子:
# the "false" shell built-in command returns 1
false | tee ; ( exit ${PIPESTATUS[0]} )
echo "return value: $?"
会给你:
返回值:1
在Ubuntu和Debian中,你可以apt-get install moreutils。这包含一个名为mispipe的实用程序,它返回管道中第一个命令的退出状态。
在普通bash中最简单的方法是使用进程替换而不是管道。有几个不同之处,但它们可能对你的用例不太重要:
当运行管道时,bash等待所有进程完成。 向bash发送Ctrl-C使其杀死管道的所有进程,而不仅仅是主要进程。 pipefail选项和PIPESTATUS变量与进程替换无关。 可能更
使用进程替换,bash只是启动进程,然后忘记它,它甚至在作业中都不可见。
除了上面提到的差异,消费者< <(生产者)和生产者|消费者本质上是相同的。
如果想要转换哪个进程是“主”进程,只需将命令和替换方向转换为生产者> >(消费者)。在你的情况下:
command > >(tee out.txt)
例子:
$ { echo "hello world"; false; } > >(tee out.txt)
hello world
$ echo $?
1
$ cat out.txt
hello world
$ echo "hello world" > >(tee out.txt)
hello world
$ echo $?
0
$ cat out.txt
hello world
正如我所说,它与管道表达式有不同之处。进程可能永远不会停止运行,除非它对管道关闭很敏感。特别是,它可能会不断向您的标准输出写入内容,这可能会令人困惑。
使用外部命令有时可能比深入研究bash的细节更简单、更清晰。管道,来自最小的进程脚本语言execline,以第二个命令*的返回代码退出,就像sh管道一样,但与sh不同的是,它允许反转管道的方向,以便我们可以捕获生产者进程的返回代码(以下都是在sh命令行上,但安装了execline):
$ # using the full execline grammar with the execlineb parser:
$ execlineb -c 'pipeline { echo "hello world" } tee out.txt'
hello world
$ cat out.txt
hello world
$ # for these simple examples, one can forego the parser and just use "" as a separator
$ # traditional order
$ pipeline echo "hello world" "" tee out.txt
hello world
$ # "write" order (second command writes rather than reads)
$ pipeline -w tee out.txt "" echo "hello world"
hello world
$ # pipeline execs into the second command, so that's the RC we get
$ pipeline -w tee out.txt "" false; echo $?
1
$ pipeline -w tee out.txt "" true; echo $?
0
$ # output and exit status
$ pipeline -w tee out.txt "" sh -c "echo 'hello world'; exit 42"; echo "RC: $?"
hello world
RC: 42
$ cat out.txt
hello world
使用管道与原生bash管道的区别与答案#43972501中使用的bash进程替换相同。
*实际上管道不退出,除非有一个错误。它执行到第二个命令中,所以是第二个命令执行返回。
(command | tee out.txt; exit ${PIPESTATUS[0]})
与@cODAR的回答不同,它返回第一个命令的原始退出码,不仅成功为0,失败为127。但是正如@Chaoran指出的,你可以直接调用${PIPESTATUS[0]}。但重要的是,所有的都放在括号里。