0

I'm trying to include an external preprocess script to my current Makefile flow. The external script location is /home/preprocessscript.sh, and content is:

cd ${path}; rm -rf *

Makefile content is:

path=/home/rubbish
clean:
  cat ${preprocessscript} >> clean.sh

I execute the command by:

make clean preprocessscript=/home/preprocessscript.sh
./clean.sh

The problem is clean.sh doesn't get path. Its content will be:

cd  ; rm -rf *

How do I pass the make variable to preprocessscript?

2 Answers 2

1

The problem is clean.sh doesn't get path. Its content will be:

cd  ; rm -rf *

I'm sorry about the contents of your home directory.

Anyway, no. The cat command will output the contents of the original file verbatim. It will not interpret them in any way, and in particular, it will not attempt to expand shell-style variable references within.

Supposing that you have faithfully represented your situation, what you actually see is that when you run the resulting clean.sh as a script, the ${path} within expands to nothing. This is not particularly surprising, because nowhere in what you've presented is an environment variable named path defined. You have defined a make variable of that name, but that's not the same thing, and it anyway would not have an effect persisting past the end of the make run to when the generated script is executed.

I guess the idea is that you want to use the original file as a template to generate content for clean.sh. There are lots of ways to do that sort of thing, but the one closest to your current attempt would probably be to output (shell) variable assignments into the generated script, like so:

path = /home/rubbish
clean:
        echo "path='$(path)'" >> clean.sh
        cat ${preprocessscript} >> clean.sh

The generated script lines would then be

path='/home/rubbish'
cd ${path}; rm -rf *

Note also that there appear to be several other issues with the general approach presented in the question, but I am focusing my comments on the specific question asked.

Sign up to request clarification or add additional context in comments.

Comments

1

You can export a make variable to the shell before running your command and use envsubst to expand the variables in the file as so:

path=/home/rubbish
clean:
    export path=${path}; envsubst < ${preprocessscript} >> clean.sh

notice that the export has to be on the same recipe line as the command. You can also pipe the output of a command to envsubst: cat ${preprocessscript} | envsubst >> clean.sh. This might be useful if you're trying to do something more complicated like only print a few lines, etc.

1 Comment

envsubst -- I learned something new today. It is probably worth noting, however, that this command is not specified by POSIX. I would expect to find it on most Linux distributions, but I'm not sure where else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.