有没有办法赶上在管道命令失败? [英] Is there a way to catch a failure in piped commands?

查看:94
本文介绍了有没有办法赶上在管道命令失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我想要达到一个例子:

Here's an example of what I'm trying to achieve:

#!/bin/bash
set -e    # abort if error
...
command1 2>&1 | command2
...

和我注意到,有时命令1 失败,但命令2不和shell脚本高高兴兴地继续...
如果我没有在这里使用的管材,设定-e 就已经足够了,但现在它不与管道那里工作......

And I notice that sometimes command1 fails but command2 does not and the shell script happily continues... if I did not have to use the pipe here, the set -e would have been sufficient but now it does not work with the pipe there...

有什么想法?
谢谢

Any thoughts? Thanks

推荐答案

既然你正在使用bash,比其他设置-e 你也可以添加设置-o pipefail 来得到你想要的结果...

Since you are using bash, other than set -e you can also add set -o pipefail to get the results you want...

这篇关于有没有办法赶上在管道命令失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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