在我的Makefile
我正在尝试检查文件是否存在,然后再永久删除。我使用此代码,但收到错误。
它有什么问题?
if [ -a myApp ]
then
rm myApp
fi
我收到以下错误消息
if [ -a myApp ]
/bin/sh: Syntax error: end of file unexpected (expecting "then")
make: *** [clean] Error 2
发布于 2013-12-13 20:41:29
看到这么多人使用shell脚本来实现这一点是很奇怪的。我一直在寻找一种使用原生makefile语法的方法,因为我是在任何目标之外编写的。您可以使用wildcard
检查文件是否存在的函数:
ifeq ($(UNAME),Darwin)
SHELL := /opt/local/bin/bash
OS_X := true
else ifneq (,$(wildcard /etc/redhat-release))
OS_RHEL := true
else
OS_DEB := true
SHELL := /bin/bash
endif
更新:
我找到了一种真正适合我的方法:
ifneq ("$(wildcard $(PATH_TO_FILE))","")
FILE_EXISTS = 1
else
FILE_EXISTS = 0
endif
发布于 2015-09-08 23:05:04
问题是当您将命令拆分为多行时。因此,您可以使用\
在如上所示的行的末尾继续,或者您可以将所有内容放在一行中,使用&&
bash中的运算符。
然后,您可以使用test
测试文件是否存在的命令,例如:
test -f myApp && echo File does exist
-f file
如果file存在并且是常规文件,则为True。
-s file
如果文件存在且大小大于零,则为True。
或者不:
test -f myApp || echo File does not exist
test ! -f myApp && echo File does not exist
test
等同于[
命令。
[ -f myApp ] && rm myApp # remove myApp if it exists
它会像你的原始示例一样工作。
请参见:help [
或者help test
有关更多语法,请参阅。
发布于 2011-04-05 22:37:29
它可能需要在行尾加上一个反斜杠才能继续(尽管这可能取决于make的版本):
if [ -a myApp ] ; \
then \
rm myApp ; \
fi;
https://stackoverflow.com/questions/5553352
复制相似问题