【问题标题】:Makefile failure when shell command does not return anything当 shell 命令不返回任何内容时,Makefile 失败
【发布时间】:2020-05-11 21:35:55
【问题描述】:

在我的 Makefile 中,我使用 grep 将未找到某些模式的文件的内容输出到另一个文件中。但是,这仅在 grep 命令有一些输出时才有效,否则无效。我宁愿在运行它时不要使用任何开关。这就是我的 Makefile 中的内容

all:
    @(grep -v -e -define file1 | grep -v -e -libfile | grep -v -e pattern3 >> file2)

如果 file1 中有不包含 -define 或 -libfile 的行,但 file1 中的所有行都具有这种模式,因此 grep 不返回任何内容然后 make 失败并出现此错误:

Makefile.test:3: recipe for target 'all' failed
make: *** [all] Error 1

该命令在 shell 中运行良好,因此这与 grep 返回 -1 并杀死 make 有关 - 有没有更好的方法来做到这一点?

【问题讨论】:

    标签: shell makefile command return-value


    【解决方案1】:

    好吧,grep 被定义为在不匹配任何内容时以非 0 代码退出;来自 grep(1) 手册页:

       Normally the exit status is 0 if a line is selected, 1 if no lines were
       selected, and 2 if an error occurred.
    

    由于 make 查看它调用的命令的退出状态以了解它是否成功,这就解释了您看到的结果。

    如果您希望它始终成功,您可以使用 true 后缀,如下所示:

    @grep -v -e -define file1 | grep -v -e -libfile | grep -v -e pattern3 >> file2; true
    

    (您不需要括号:make 已经在其自己的子 shell 中调用了每个配方行)。

    【讨论】:

    • 也可以在配方的开头添加-,让make知道错误应该被忽略。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2013-08-27
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2014-01-25
    相关资源
    最近更新 更多