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
但是,当输入任何内容(例如“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!
我该如何解决这个问题???
I have the following code inside a 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:
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???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
直接的问题是 Make 本身对
$
的解释与 shell 不同。尝试:双
$$
对 Make 转义$
,因此它将单个$
传递到 shell。另请注意,您需要使用\
行延续,以便整个序列作为一个 shell 脚本执行,否则 Make 将为每一行生成一个新 shell。这意味着您读取
的任何内容都会在 shell 退出后立即消失。我还想说,一般来说,提示从 Makefile 进行交互式输入的情况并不常见。您最好使用命令行开关来指示 Web 服务器根目录。
The immediate problem is that Make itself interprets the
$
differently than the shell does. Try: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 new shell for each line. That means that anything youread
will disappear as soon as its shell exits.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.
使用 .ONESHELL 使多行命令比使用 ';' 更容易阅读和 '\' 分隔行:
我没有足够的业力来发表评论,因此有一个答案(应该是对已接受答案的评论):
Using .ONESHELL makes multiline commands easier to read then using ';' and '\' to separate lines:
I don't have enough karma to post comments, therefore an answer (which should've been a comment to the accepted answer):