开发者

Bash: how to detect error in pipe

开发者 https://www.devze.com 2023-03-31 09:39 出处:网络
I have commands like this: COMMAND1 && COMMAND2 | COMMAND3 | COMMAND4 && COMMAND5 I want to make sure all of the COMMAND 1-5 are successful.Is there an easy way to achieve this?By d

I have commands like this:

COMMAND1 &&
COMMAND2 | COMMAND3 | COMMAND4 &&
COMMAND5

I want to make sure all of the COMMAND 1-5 are successful. Is there an easy way to achieve this? By doing research, I fo开发者_开发知识库und PIPESTATUS can be used, but yield to some very complicated commands like

COMMAND1 &&
COMMAND2 | COMMAND3 | COMMAND4 &&
($PST=("${PIPESTATUS[@]}") && (exit ${PST[0]}) && (exit ${PST[1]})) &&
COMMAND5

Is there a way to do it easily?

BTW: I used (exit n) to get a command that does nothing but exit with status n. Is there a UNIX command that does this directly, like true and false?

Thanks.


Try using set -o pipefail. This ensures that the error code of the pipeline is the error code of the last process with an error.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号