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

标签 bash makefile

如果我有以下 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/

相关文章:

bash - grep匹配后获取下一个单词

bash - 为什么 wget 给我两个不同的总下载时间?

python - 在 python 脚本中使用 bash 命令时遇到问题

android - Gradle 的外部依赖

makefile - 是否有一种**简单**的方法可以使用 Makefile 项目将目标文件创建重定向到指定目录?

makefile - NIST 统计测试套件

c - makefile gcc 自定义动态 lib 与 .so 文件的链接问题

bash - 如何在用户数据bash代码中获取CloudFormation资源值

bash - grep- 为什么反向引用周围必须有单词边界?

c++ - 如何使用来自不同目录的make文件中的文件