bash Makefile 错误退出并显示一条消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44061611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Makefile error exit with a message
提问by Basile Starynkevitch
Is there any way to output a error message when the error happens.Let's go into details,I want to know whether one file content equal to another or not.If not ,make
should exit and output a error message.
有没有什么方法可以在错误发生时输出错误信息。详细说一下,我想知道一个文件内容是否等于另一个文件内容。如果不是,make
应该退出并输出错误信息。
test:
cmp --silent tmp/test.txt tmp/test_de.txt || (error DecryptFile is different from original)
When tmp/test.txt
does not equal to tmp/test_de.txt
,the output is:
当tmp/test.txt
不等于时tmp/test_de.txt
,输出为:
cmp --silent tmp/test.txt tmp/test_de.txt | (error DecryptFile is different from original)
/bin/sh: 1: error: not found
makefile:38: recipe for target 'test' failed
make: *** [test] Error 127
/bin/sh: 1: error: not found
/bin/sh: 1: 错误:未找到
The result isn't what I want.I just want like this kind of error message:
结果不是我想要的。我只是想要这样的错误信息:
makefile:38: *** missing separator. Stop.
回答by torbatamas
You can use exit
. the (
and )
can enclose more than one command:
您可以使用exit
. 的(
和)
能够包围一个以上的命令:
cmp --silent tmp/test.txt tmp/test_de.txt || (echo "DecryptFile is different from original"; exit 1)
回答by Basile Starynkevitch
Perhaps the error
refers to the GNU Make built-in error functionbut then you should write $(error
....)
instead of just (error
....)
and you cannot use it that way (in a shell command). So you really should use echo
and exit
as answered by tobatamas. Perhaps you might redirect the echo
to stderr(e.g. echo message 2>&1
or echo message > /dev/stderr
)
也许error
指的是GNU Make 内置错误函数,但是您应该编写$(error
....)
而不仅仅是(error
....)
并且您不能那样使用它(在 shell 命令中)。所以,你真的应该使用echo
,并exit
作为由tobatamas回答。也许您可能会将其重定向echo
到stderr(例如echo message 2>&1
或echo message > /dev/stderr
)
The $(error
....)
builtin could be (and often is) used with GNU make conditionals.
该$(error
....)
是GNU make使用内置的可能是(而且经常是)条件语句。