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

bal*_*ton 15 bash shell makefile

我在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
Run Code Online (Sandbox Code Playgroud)

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

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

asd
oot_path
Error: Could not find that location!
Run Code Online (Sandbox Code Playgroud)

当我期望看到的是:

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

asd
asd
Error: Could not find that location!
Run Code Online (Sandbox Code Playgroud)

我该如何解决???

Gre*_*ill 21

直接的问题是,Make本身的解释$与shell的解释不同.尝试:

    echo "What is the root directory of your webserver? Eg. ~/Server/htdocs"; \
    read root_path; \
    echo $$root_path
Run Code Online (Sandbox Code Playgroud)

双重$$转义$为Make,因此它将单个$传递给shell.另请注意,您将需要使用\行继续,以便整个序列作为一个shell脚本执行,否则Make将为每一行生成一个新的 shell.这意味着任何read一旦它的外壳退出就会消失的东西.

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

  • 借调"提示互动输入是不常见的"的想法.每次键入`make`时,您都不希望回答提示. (4认同)

cro*_*e2u 6

使用 .ONESHELL 使多行命令比使用 ';' 更容易阅读 和 '\' 来分隔行:

.ONESHELL:
my-target:
  echo "What is the root directory of your webserver? Eg. ~/Server/htdocs"
  read root_path
  echo $$root_path
Run Code Online (Sandbox Code Playgroud)

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