shell命令不返回任何内容时,Makefile失败

问题描述 投票:0回答:1

在我的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个回答
0
投票

嗯,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已经在其自己的子外壳中调用了每个配方行)。

© www.soinside.com 2019 - 2024. All rights reserved.