In my Makefile I want to check if multiple environments variables are set.
But I don't want to write multiple ifndef
for each one. I just want an array of variables to make it reusable.
check-variables:
for var in var1 var2 var3 ; do \
if [ -z "$$var" ] ; then \
echo "$$var is not set"; exit 1; \
fi \
done
But it's not working...
This isn't a make issue, it's a shell issue. If you ran that script at your shell prompt (changing $$
back to $
as make will do, of course) it wouldn't work either. Until you can get the command to work at your shell prompt, you can't get it to work in a makefile.
The shell command would be:
for var in var1 var2 var3 ; do \
if [ -z "$var" ] ; then \
echo "$var is not set"; exit 1; \
fi \
done
You can see why this doesn't do what you want: you're checking the shell variable var
every time. What you're trying to do is check the value of the variable which is named by the value of $var
. To do this you need eval (the shell's eval, not make's eval
function):
for var in var1 var2 var3 ; do \
eval test -n \"\$$var\" \
|| { echo "$var is not set"; exit 1; }; \
done
You should discover that the above will work in the shell, then you need to put it back into the makefile (and double all the $
).