This succeeds:
$ for i in false true; do echo $i; $i; doneThis fails
false
true
$ echo $?
0
$ for i in true false; do echo $i; $i; doneWhen does this matter? In Makefiles. Suppose you have a make rule that looks like this:
true
false
$ echo $?
1
foo:A failure of one of the subdirectory makes will not cause the make to fail!
for dir in $(SUBDIRS); do $(MAKE) -C $$dir; done
How do you solve this? There are several sophisticated ways, but the dead-easy way is to start the Makefile like this:
SHELL := /bin/bash -eThis will cause all shell commands to be executed with the -e flag, which causes any simple command to terminate the shell.
$ ( set -e; for i in false true; do echo $i; $i; done )(I have to put the command and the set -e in parens here -- though not in the Makefile -- because any failure causes the command to fail and the shell to exit, so the following "echo $?" would never be reached.
false
$ echo $?
1
No comments:
Post a Comment