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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:10:13  来源:igfitidea点击:

Makefile error exit with a message

bashshellmakefilegnu-make

提问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 ,makeshould 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.txtdoes 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 errorrefers 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 echoand exitas answered by tobatamas. Perhaps you might redirect the echoto stderr(e.g. echo message 2>&1or echo message > /dev/stderr)

也许error指的是GNU Make 内置错误函数,但是您应该编写$(error....)而不仅仅是(error....)并且您不能那样使用它(在 shell 命令中)。所以,你真的应该使用echo,并exit作为由tobatamas回答。也许您可能会将其重定向echostderr(例如echo message 2>&1echo message > /dev/stderr

The $(error....)builtin could be (and often is) used with GNU make conditionals.

$(error....)是GNU make使用内置的可能是(而且经常是)条件语句