庆典:管道输出和捕获退出状态 [英] bash: pipe output AND capture exit status

查看:143
本文介绍了庆典:管道输出和捕获退出状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行的bash shell中长时间运行的命令,都捕获它的退出状态和三通的输出。

I want to execute a long running command in bash shell, and both capture its exit status, and tee its output.

所以我这样做

command | tee out.txt
ST=$?

问题是变量ST抓住三通的退出状态,而不是命令。我怎样才能解决这个问题?

The problem is that the variable ST captures the exit status of tee and not of command. How can I solve this?

请注意这个命令是长时间运行和输出重定向到一个文件,查看它以后是不是对我来说是很好的解决方案。

Note that command is long running and redirecting the output to a file to view it later is not a good solution for me.

推荐答案

有一个名为内部猛砸变量 $ PIPESTATUS ;它是持有命令你最后的前台管道的每个命令的退出状态的数组。

There is an internal Bash variable called $PIPESTATUS; it’s an array that holds the exit status of each command in your last foreground pipeline of commands.

<command> | tee out.txt ; test ${PIPESTATUS[0]} -eq 0

这篇关于庆典:管道输出和捕获退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆