gpt4 book ai didi

for-loop - Makefile:for 循环并在出错时中断

转载 作者:行者123 更新时间:2023-12-04 22:05:49 25 4
gpt4 key购买 nike

我有一个带有 for 循环的 Makefile。问题是当循环内发生错误时,执行继续。

SUBDIRS += $(shell ls -d */ | grep amore)

# breaks because can't write in /, stop execution, return 2
test:
mkdir /
touch /tmp/zxcv

# error because can't write in / but carry on, finally return 0
tests:
@for dir in $(SUBDIRS); do \
mkdir / ; \
touch /tmp/zxcv ; \
done;

遇到错误时如何让循环停止?

最佳答案

要么添加 || exit 1对每个可能失败的调用或您执行 set -e在规则的开头:

tests1:
@dir in $(SUBDIRS); do \
mkdir / \
&& touch /tmp/zxcv \
|| exit 1; \
done

tests2:
@set -e; \
for dir in $(SUBDIRS); do \
mkdir / ; \
touch /tmp/zxcv ; \
done

关于for-loop - Makefile:for 循环并在出错时中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16060338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com