管道的四种状态linux,linux – 在makefile中处理管道及其退出状态的最佳方式

如果命令失败,例如

gcc,它将退出…

gcc

gcc: fatal error: no input files

compilation terminated.

make: *** [main.o] Error 4

但是,如果我有一个管道,则管道中最后一个命令的退出状态将被采用.例如,gcc |猫不会因为猫成功而失败.

我知道整个管道的退出代码存储在PIPESTATUS数组中,我可以用${PIPESTATUS [0]}获取错误代码4.我应该如何构造我的makefile来处理一个管道命令,并正常退出失败?

在评论中,另一个例子是gcc | grep的东西在这里,我认为最需要的行为仍然是gcc,只有gcc导致失败,如果没有找到任何东西,则不会grep.

404_13@

你应该可以告诉make使用bash而不是sh,并且bash将set -o pipefail设置为使得它在管道中首次出现故障时退出.

在GNU Make 3.81(大概早些虽然我不知道肯定),你应该能够用SHELL = / bin / bash -o pipefail这样做.

在GNU Make 3.82(和更新版本)中,您应该可以使用SHELL = / bin / bash和.SHELLFLAGS = -o pipefail -c(尽管我不知道如何添加-c到最后是必要的,如果make将为您添加,即使指定.SHELLFLAGS.

从bash手册页:

The return status of a pipeline is the exit status of the last command,unless the pipefail option is enabled. If pipefail is enabled,the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status,or zero if all commands exit successfully. If the reserved word ! precedes a pipeline,the exit status of that pipeline is the logical negation of the exit status as described above. The shell waits for all commands in the pipeline to terminate before returning a value.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值