bash Makefile - 为什么读取命令不读取用户输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3743793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Makefile - Why is the read command not reading the user input?
提问by balupton
I have the following code inside a 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
However when typing anything (eg. "asd") this is what gets returned:
然而,当输入任何东西(例如“asd”)时,这就是返回的内容:
What is the root directory of your webserver? Eg. ~/Server/htdocs
asd
oot_path
Error: Could not find that location!
When what I would expect to see would be:
当我期望看到的是:
What is the root directory of your webserver? Eg. ~/Server/htdocs
asd
asd
Error: Could not find that location!
How do I fix this???
我该如何解决???
回答by Greg Hewgill
The immediate problem is that Make itself interprets the $differently than the shell does. Try:
直接的问题是 Make 本身的解释$与 shell 不同。尝试:
echo "What is the root directory of your webserver? Eg. ~/Server/htdocs"; \
read root_path; \
echo $$root_path
The double $$escapes the $for Make, so it passes the single $through to the shell. Note also that you will need to use \line continuations so that the whole sequence is executed as one shell script, otherwise Make will spawn a newshell for each line. That means that anything you readwill disappear as soon as its shell exits.
double$$转义$for Make,因此它将 single$传递到 shell。另请注意,您将需要使用\连续行,以便将整个序列作为一个 shell 脚本执行,否则 Make 将为每一行生成一个新的shell。这意味着read只要它的外壳退出,你的任何东西都会消失。
I would also say that in general, prompting for interactive input from a Makefile is uncommon. You might be better off using a command line switch to indicate the web server root directory.
我还要说,一般来说,从 Makefile 提示交互式输入是不常见的。您最好使用命令行开关来指示 Web 服务器根目录。
回答by crocodile2u
Using .ONESHELL makes multiline commands easier to read then using ';' and '\' to separate lines:
使用 .ONESHELL 使多行命令比使用 ';' 更容易阅读 和 '\' 分隔行:
.ONESHELL:
my-target:
echo "What is the root directory of your webserver? Eg. ~/Server/htdocs"
read root_path
echo $$root_path
I don't have enough karma to post comments, therefore an answer (which should've been a comment to the accepted answer):
我没有足够的业力来发表评论,因此是一个答案(应该是对已接受答案的评论):

