忽略Bash pipefail出现错误代码141 [英] Ignoring Bash pipefail for error code 141

查看:435
本文介绍了忽略Bash pipefail出现错误代码141的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置bash pipefail 选项(通过 set -o pipefail )允许脚本失败,零错误被捕获在管道的任何步骤中都有非零错误。



但是,我们正在运行到 SIGPIPE 错误(错误代码141),其中数据写入不再存在的管道。



有没有办法将bash设置为忽略 SIGPIPE 错误,或者是否有一种方法来编写一个处理所有错误状态代码的错误处理程序,例如0和141?



例如,在Python中,我们可以添加:

  signal.signal(signal.SIGPIPE,signal.SIG_DFL)

将默认行为应用于 SIGPIPE 错误:忽略它们(参见 http://编码。 derkeiler.com/Archive/Python/comp.lang.python/2004-06/3823.html )。



是否有bash中有一些类似的选项?

解决方案

陷阱命令可以让你指定在遇到信号时运行的命令。要忽略信号,请传递空字符串:

 陷阱PIPE 
/ pre>

Setting the bash pipefail option (via set -o pipefail) allows the script to fail if a non-zero error is caught where there is a non-zero error in any step of a pipe.

However, we are running into SIGPIPE errors (error code 141), where data is written to a pipe that no longer exists.

Is there a way to set bash to ignore SIGPIPE errors, or is there an approach to writing an error handler that will handle all error status codes but, say, 0 and 141?

For instance, in Python, we can add:

signal.signal(signal.SIGPIPE, signal.SIG_DFL) 

to apply the default behavior to SIGPIPE errors: ignoring them (cf. http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-06/3823.html).

Is there some similar option available in bash?

解决方案

The trap command lets you specify a command to run when encountering a signal. To ignore a signal, pass the empty string:

trap '' PIPE

这篇关于忽略Bash pipefail出现错误代码141的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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