Reputation: 1160
My team has a target at the end of a Makefile that looks like this:
# Fix permissions and ownership problems (only run this in development!)
fixperms:
@if [ $(OS_NAME) == "Linux" ]; then \
@sudo chown --recursive php:php .
@sudo chmod --recursive 0777 var/
fi
@if [ $(OS_NAME) == "Darwin" ]; then \
@sudo chown --recursive php:php .
@sudo chmod -R 0777 var/
fi
... and it includes a newline at the end. When I run make fixperms
(on my macOS machine -- I haven't tried it on Linux), I get the following message:
/bin/sh: -c: line 1: syntax error: unexpected end of file
make: *** [fixperms] Error 2
How can I fix this?
===
Edit 1: I have also tried it without the newline -- the result is the same.
===
Edit 2: I have also tried rearranging the Makefile so that a different target is at the end. Running make fixperms
still results in the same error. Very strange.
Upvotes: 1
Views: 5692
Reputation: 101051
I'm assuming you have TABs indenting your recipe here that you don't show.
The problem is you only have a backslash at the end of the first line, so that's the only line that's "continued". Make invokes every logical line in a recipe in a separate shell, so this:
fixperms:
@if [ $(OS_NAME) == "Linux" ]; then \
@sudo chown --recursive php:php .
@sudo chmod --recursive 0777 var/
fi
Runs the following shell commands:
/bin/sh -c 'if [ $(OS_NAME) == "Linux" ]; then @sudo chown --recursive php:php .'
/bin/sh -c 'sudo chmod --recursive 0777 var/'
/bin/sh -c 'fi'
You can see that these are illegal shell scripts; you get an error in the first one because you're missing the fi
.
You should write this as one shell command script, with backslashes to ensure the next line is included in the logical line:
fixperms:
@if [ $(OS_NAME) == "Linux" ]; then \
sudo chown --recursive php:php .; \
sudo chmod --recursive 0777 var/; \
fi
@if [ $(OS_NAME) == "Darwin" ]; then \
sudo chown --recursive php:php .; \
sudo chmod -R 0777 var/; \
fi
(note take out the @
in the middle of the script as well as those are makefile recipe metacharacters and only are recognized at the beginning of a recipe).
I actually think you should remove all the @
. It's always a fatal mistake to add them at least until your makefile is 100% working, because you're throwing away critical information that would otherwise help you debug. For example, if you'd not had them here you'd see make print out the line it was going to invoke and it might have helped you realize what the problem was.
You might consider this: http://make.mad-scientist.net/managing-recipe-echoing/
Upvotes: 2
Reputation: 1160
I never was able to get shell conditionals to work. I switched to using Makefile conditionals, which seems to have done the job. So I now have this:
# Fix permissions and ownership problems (only run this in development!)
fixperms:
ifeq ("$(OS_NAME)", "Linux")
sudo chown --recursive php:php . ; sudo chmod --recursive 0777 var/
endif
ifeq ("$(OS_NAME)", "Darwin")
sudo chown -R php:php . ; sudo chmod -R 0777 var/
endif
... which seems to work well.
Upvotes: 1