Make 无法检查目录是否存在
我用谷歌搜索了这个,但我不明白为什么 Bash 会抱怨以下代码来检查目录是否存在:
test.mk
#!/bin/bash
MYDIR="dl"
all:
if [ ! -d $MYDIR ]; then
#if [ ! -d "${MYDIR}" ]; then
#if [ ! -d ${MYDIR} ]; then
#Here
fi
make -f test.mk
if [ ! -d YDIR ]; then
/bin/sh: Syntax error: end of file unexpected
make: *** [all] Error 2
有人知道它为什么失败吗?为什么它调用 /bin/sh 而不是 /bin/bash?谢谢。
编辑:与 Bash 不同,make 不支持多行块。这是工作代码:
MYDIR="dl"
all:
if [ ! -d ${MYDIR} ]; then\
echo "Here";\
else\
echo "There";\
fi
I googled for this, but I can't figure out why Bash complains with the following code to check if a directory exists:
test.mk
#!/bin/bash
MYDIR="dl"
all:
if [ ! -d $MYDIR ]; then
#if [ ! -d "${MYDIR}" ]; then
#if [ ! -d ${MYDIR} ]; then
#Here
fi
make -f test.mk
if [ ! -d YDIR ]; then
/bin/sh: Syntax error: end of file unexpected
make: *** [all] Error 2
Does someone know why it fails? And why does it call /bin/sh instead of /bin/bash? Thank you.
Edit: unlike Bash, make doesn't support multi-line block. Here's working code:
MYDIR="dl"
all:
if [ ! -d ${MYDIR} ]; then\
echo "Here";\
else\
echo "There";\
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在顶部插入的
#!/bin/bash
shebang 没有用,它被make
视为注释。make
默认将命令发送到/bin/sh
。要指定不同的 shell,请使用宏SHELL = /bin/bash
。此外,您需要转义变量:
我不确定 make 是否可以处理多行语句,因此尝试将所有 if 块放在一行中。
The
#!/bin/bash
shebang that you inserted at top is useless, and it is treated bymake
as a comment.make
sends by default commands to/bin/sh
. To specify a different shell, use the macroSHELL = /bin/bash
.Moreover, you need to escape your variable:
I'm not sure if
make
can handle multi-line statements, so try to put all theif
block in a line.您将
test.mk
提供给make
,而不是bash
。然后make
将单独的行发送到 shell,而不是整个块。make
使用其SHELL
宏来确定要使用哪个 shell。您可以覆盖它以使其使用bash
。您得到
YDIR
的原因是make
对于变量插值有愚蠢的规则。写入$(MYDIR)
,而不是$MYDIR
。You're feeding
test.mk
tomake
, not tobash
. Thenmake
sends individual lines to the shell, not whole blocks.make
uses itsSHELL
macro to determine which shell to use. You can override it to make it usebash
.The reason why you're getting
YDIR
is thatmake
has silly rules about variable interpolation. Write$(MYDIR)
, not$MYDIR
.尝试支撑你的变量:
try bracing your variable: