Makefile - 为什么读取命令不读取用户输入?

发布于 2024-09-19 15:30:54 字数 630 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日裸衫吸 2024-09-26 15:30:54

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

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

$$ 对 Make 转义 $,因此它将单个 $ 传递到 shell。另请注意,您需要使用 \ 行延续,以便整个序列作为一个 shell 脚本执行,否则 Make 将为每一行生成一个 shell。这意味着您读取的任何内容都会在 shell 退出后立即消失。

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

The immediate problem is that Make itself interprets the $ differently than the shell does. Try:

    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 new shell for each line. That means that anything you read 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.

懒的傷心 2024-09-26 15:30:54

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

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

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

Using .ONESHELL makes multiline commands easier to read then using ';' and '\' to separate lines:

.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):

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文