I have a Makefile which runs a program which on success return a non-zero value, and on failure return another non-zero value. I know that I can ignore the exit status by prefixing the command with -, but that does not work because I need to know if the command succeeded.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can test the returned value on a second command on the same Makefile line, using the shell $?
variable that contains the last returned value.
For example with the false
command that would obviously stop the compilation:
test:
/bin/false ; /usr/bin/test "$$?" -eq 1 # <-- make does not stop here
/bin/echo "Continues ..."
/bin/false # <-- make stops here
回答2:
Use
command || [ $$? -eq v ]
as your command, substituting command with the command, and v with the value returned on success.
(This is just a more compact version of Didier Trosset's answer.)
回答3:
Depending on how the tool behaves on fail, you could just check for the existence of the output file. something like:
@if test ! -f $(FILE); then exit 2; fi