I have a Makefile with a for loop. The problem is that when an error occurs within the loop, the execution carry on.
SUBDIRS += $(shell ls -d */ | grep amore)
# breaks because can't write in /, stop execution, return 2
test:
mkdir /
touch /tmp/zxcv
# error because can't write in / but carry on, finally return 0
tests:
@for dir in $(SUBDIRS); do \
mkdir / ; \
touch /tmp/zxcv ; \
done;
How to have the loop stop when it encounters an error ?
Either you add a || exit 1
to every call that potentially fails or you do a set -e
at the beginning of the rule:
tests1:
@dir in $(SUBDIRS); do \
mkdir / \
&& touch /tmp/zxcv \
|| exit 1; \
done
tests2:
@set -e; \
for dir in $(SUBDIRS); do \
mkdir / ; \
touch /tmp/zxcv ; \
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With