3

I have a for-loop in a shell script a la:

#!/bin/bash
set -u
set -e
for l in sh rb py php java cs; do
  (cd $l; ./run-tests.sh)
done

The intent is to have the for-loop die in a fire when any one of the sub-commands similarly errors out.

Now, I have a work-around in place: (cd $l; ./run-tests.sh) || die "Message here" along with a suitable definition of die. However, I'm genuinely curious why the for-loop doesn't self-terminate per the expectation set by the "set -e" command? Ideally I'd like to not special-case every for-loop like that. :)

Oliver Salzburg
  • 87,539
  • 63
  • 263
  • 308

2 Answers2

1

The reason set -e does not exit immediately if ./run-test.sh fails is that the lines:

for l in sh rb py php java cs; do
  (cd $l; ./run-tests.sh)
done

for a compound statement. The status of this compound statement is the status of the last run of ./run-tests.sh.

Your work around sounds good to me.

R Sahu
  • 236
0

I do not have comment rights, so posting here. It depends on the script in run-tests.sh. Are you sure this gives a non-zero value:

./run-tests.sh

echo $?

beginer
  • 257