Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile: for loop and break on error

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 ?

like image 714
Barth Avatar asked Apr 17 '13 12:04

Barth


1 Answers

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
like image 167
Michael Wild Avatar answered Sep 30 '22 06:09

Michael Wild