views:

91

answers:

1

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???

+3  A: 

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.

Greg Hewgill
Seconding the "prompting for interactive input is uncommon" idea. You don't want to have to answer a prompt every time you type `make`.
John Kugelman
I'd s/uncommon/an abomination/. That's what `configure` scripts and make variables are for
Daenyth
Thanks! I always thought Makefiles were just glorified shell scripts, but this definitely shows otherwise. Will definitely have to reconsider my use cases with them. @Daenyth, what do you mean by configure scripts? Are they something special???
balupton
@balupton: Plain make has some trouble dealing with things that change across platforms and installations. Especially the original `make` which was rather less powerful than GNU make. A convention arose to write configure scripts which would handle the platform specific setup. That tools for writing those files developed. You can look at `autoconf`, if you want, but it is widely considered a nightmare. If you have only a few targets and/or a few platform specific configuration, there are other ways of dealing with these issues.
dmckee
Cool thanks. I've moved it into a php installer script that runs on the command line - eventually will have a webpage interface. As it is for configuring a web application. Autoconf seems too out there for this.
balupton