I have errexit
set inside of a Hudson job so that it will die as soon as some component command fails. This has generally been working great until I added some error checking, and now it's not behaving as I would expect it to.
$ bash -version
GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu)
Some sample code:
/tmp/foo
$ cat /tmp/foo
errexit_test()
{
set -o errexit
BIN=$1; shift
/tmp/bar $BIN
/tmp/baz
}
/tmp/bar
$ cat /tmp/bar
#!/bin/bash
BIN=$1
/bin/$BIN
EXIT_CODE=$?
echo "done bar"
exit $EXIT_CODE
/tmp/baz
$ cat /tmp/baz
#!/bin/bash
echo "done baz"
Running them inside of a shell started with -e
gives the expected results:
no error
$ echo $SHLVL
1
$ bash -e
$ echo $SHLVL
2
$ . /tmp/foo
$ errexit_test true
done bar
done baz
$ echo $SHLVL
2
with error
$ echo $SHLVL
2
$ errexit_test false
done bar
$ echo $SHLVL
1
If try to intercept the error, I succeed -- sort of. The inner function no longer stops executing as I think it should:
$ errexit_test false || /bin/true
done bar
done baz
$ echo $SHLVL
2
What I would like is for the function to still halt execution immediately, but the calling program should continue or not depending on the exit code from the right side of the ||
.
There seems to be something else going on that I don't understand. Add in another script:
/tmp/extra
$ cat /tmp/extra
#!/bin/bash
echo "in extra"
and that behaves as expected when combined with a command/program:
$ /tmp/extra
in extra
$ /bin/true || /tmp/extra
$ /bin/false || /tmp/extra
in extra
but not when combined with the function:
$ errexit_test false || /tmp/extra
done bar
done baz
These two issues may be unrelated, but I feel that they are, though I can't figure out how or why.