测试文件是否存在于makefile目标中,如果不存在则退出 [英] Testing if a file exists in makefile target, and quitting if not present
问题描述
如果文件不存在,是否可以退出并出现错误情况?我目前正在做这样的事情:
Is there a way to exit with an error condition if a file does not exist? I am currently doing something like this:
all: foo
foo:
test -s /opt/local/bin/gsort || echo "GNU sort does not exist! Exiting..." && exit
运行make
运行all
目标,该目标运行foo
.
Running make
runs the all
target, which runs foo
.
期望的是,如果test -s
条件失败,那么将执行echo/exit
语句.
The expectation is that if the test -s
conditional fails, then the echo/exit
statements are executed.
但是,即使存在/usr/bin/gsort
,我也可以得到echo
语句的结果,但是exit
命令不会运行.这与我希望实现的目标相反.
However, even if /usr/bin/gsort
exists, I get the result of the echo
statement but the exit
command does not run. This is the opposite of what I am hoping to accomplish.
做上述事情的正确方法是什么?
What is the correct way to do something like the above?
推荐答案
exit
alone returns the status of the last command executed. In this case, it returns zero, which means everything is ok.
这是因为||
和&&
具有具有相同的优先级,然后外壳程序将命令解释为好像是写出的命令
This is, because ||
and &&
have equal precedence, and the shell interprets the command as if it were written
( test ... || echo ... ) && exit
如果要发出故障信号,则必须以非零值退出,例如exit 1
.
如果要回显和退出,只需按;
If you want to signal failure you must exit with a non zero value, e.g. exit 1
.
And if you want to echo and exit, just put the commands in sequence separated by ;
all: foo
foo:
test -s /opt/local/bin/gsort || { echo "GNU sort does not exist! Exiting..."; exit 1; }
这篇关于测试文件是否存在于makefile目标中,如果不存在则退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!