Makefile - 为什么read命令没有读取用户输入?

时间:2010-09-18 22:38:17

标签: bash makefile shell

我在Makefile中有以下代码:

# Root Path
echo "What is the root directory of your webserver? Eg. ~/Server/htdocs" ;
read root_path ;
echo $root_path ;
if [ ! -d $root_path ] ; then \
    echo "Error: Could not find that location!" ; exit 1 ; \
fi

但是当输入任何东西时(例如“asd”),这就是返回的内容:

What is the root directory of your webserver? Eg. ~/Server/htdocs

asd
oot_path
Error: Could not find that location!

当我期望看到的是:

What is the root directory of your webserver? Eg. ~/Server/htdocs

asd
asd
Error: Could not find that location!

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:21)

直接问题是Make本身以不同于shell的方式解释$。尝试:

    echo "What is the root directory of your webserver? Eg. ~/Server/htdocs"; \
    read root_path; \
    echo $$root_path

$$转义为Make的$,因此它将单个$传递给shell。另请注意,您需要使用\行继续,以便整个序列作为一个shell脚本执行,否则Make将为每一行生成 new shell。这意味着一旦你的shell退出,你read将会消失。

我还会说,一般来说,提示Makefile的交互式输入并不常见。您可能最好使用命令行开关来指示Web服务器根目录。

答案 1 :(得分:2)

使用.ONESHELL可以使多行命令更易于阅读,然后使用';'和'\'分隔行:

.ONESHELL:
my-target:
  echo "What is the root directory of your webserver? Eg. ~/Server/htdocs"
  read root_path
  echo $$root_path

我没有足够的业力来发表评论,因此没有答案(应该是对已接受答案的评论)