Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. ~/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???

like image 236
balupton Avatar asked Sep 18 '10 22:09

balupton


People also ask

How do I read user input in makefile?

If you want to get user input from within a Makefile, ensure you run the read command in one line. It turns out that each line is run in its own subshell. So if you read in user input, ensure you have semicolons and backslashes to ensure commands are run in the same subshell.

Which command is used to get input from the user get accept read?

Explanation: The scanf statement in the c programming language is used to accept the data from the user during the program execution. In C++ we can used the cin statement which is accpet the data from the user in the execution time.

What is the function of read command?

The read command reads one line from standard input and assigns the values of each field in the input line to a shell variable using the characters in the IFS (Internal Field Separator) variable as separators.

What command reads user?

#!/bin/bash. # Read the user input. echo "Enter the user name: " read first_name.


1 Answers

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.

like image 159
Greg Hewgill Avatar answered Sep 20 '22 17:09

Greg Hewgill