测试文件是否存在于makefile目标中,如果不存在则退出 [英] Testing if a file exists in makefile target, and quitting if not present

查看:719
本文介绍了测试文件是否存在于makefile目标中,如果不存在则退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果文件不存在,是否可以退出并出现错误情况?我目前正在做这样的事情:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆