开发者

Makefile - Why is the read command not reading the user input?

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. 开发者_如何学Python~/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???


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜