gpt4 book ai didi

bash - 使用循环时如何使makefile错误退出?

转载 作者:行者123 更新时间:2023-11-29 08:52:24 25 4
gpt4 key购买 nike

如果我有以下 bash 命令:

for i in ./ x ; do ls $i ; done && echo OK

执行“ls ./”,然后执行“ls x”,失败(x 丢失)并且未打印 OK。

如果

for i in x ./ ; do ls $i ; done && echo OK

那么即使“ls x”失败了,因为for循环中的最后一条语句成功了,然后打印OK。这是在 makefile 中使用 shell for 循环时的问题:

x:
for i in $(LIST) ; do \
cmd $$i ;\
done

如果 cmd 的任何单个执行失败,我如何使 make 失败?

最佳答案

使用break命令在命令失败时终止循环

x:
for i in $(LIST) ; do \
cmd $$i || break ;\
done

不过,这不会使 makefile 中止。您可以改为使用非零代码的 exit:

x:
for i in $(LIST) ; do \
cmd $$i || exit 1 ;\
done

关于bash - 使用循环时如何使makefile错误退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19436631/

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