I want to do something like:
for i in *
do
if test -d $i
then
cd $i; make clean; make; cd -;
fi;
done
And this
You can use Make itself to achieve what you're looking for:
SUBDIRS := $(wildcard */.)
.PHONY : all $(SUBDIRS)
all : $(SUBDIRS)
$(SUBDIRS) :
$(MAKE) -C $@ clean all
Make will break execution in case when any of your target fails.
To support arbitrary targets:
SUBDIRS := $(wildcard */.) # e.g. "foo/. bar/."
TARGETS := all clean # whatever else, but must not contain '/'
# foo/.all bar/.all foo/.clean bar/.clean
SUBDIRS_TARGETS := \
$(foreach t,$(TARGETS),$(addsuffix $t,$(SUBDIRS)))
.PHONY : $(TARGETS) $(SUBDIRS_TARGETS)
# static pattern rule, expands into:
# all clean : % : foo/.% bar/.%
$(TARGETS) : % : $(addsuffix %,$(SUBDIRS))
@echo 'Done "$*" target'
# here, for foo/.all:
# $(@D) is foo
# $(@F) is .all, with leading period
# $(@F:.%=%) is just all
$(SUBDIRS_TARGETS) :
$(MAKE) -C $(@D) $(@F:.%=%)
You can check whether the make
has exited successfully by examining its exit code via the $?
variable, and then have a break
statement:
...
make
if [ $? -ne 0 ]; then
break
fi