Ignoring Bash pipefail for error code 141

你说的曾经没有我的故事 提交于 2019-12-05 01:44:18

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

trap '' PIPE

I handle this on a per-pipeline basis by tacking on an || if ... statement to swallow exit code 141 but still bubbling up any other errors:

pipe | that | fails || if [[ $? -eq 141 ]]; then true; else exit $?; fi

There isn't a way that I know of to do this for the whole script. It would be risky in general, since there's no way to know that a child process didn't return 141 for a different reason.

But you can do this on a per-command basis. The || operator suppresses any errors returned by the first command, so you can do something like:

set -e -o pipefail
(cat /dev/urandom || true) | head -c 10 | base64
echo 'cat exited with SIGPIPE, but we still got here!'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!