Não sei por que os valores $?
e $!
na função foo
não pertencem ao CHLD
em questão, mas as seguintes correções parecem contornar o problema, usando jobs -p
dentro a função foo
, assim:
#!/usr/bin/env bash
set -m # allow for job control
EXIT_CODE=0 # exit code of overall script
function foo() {
for job in 'jobs -p'; do
echo "PID => ${job}"
if ! wait ${job} ; then
echo "At least one test failed with exit code => $?" ;
EXIT_CODE=1;
fi
done
}
trap 'foo' CHLD
DIRN=$(dirname "$0")
commands=(
"{ echo 'foo' && exit 4; }"
"{ echo 'bar' && exit 3; }"
"{ echo 'baz' && exit 5; }"
)
clen='expr "${#commands[@]}" - 1' # get length of commands - 1
for i in 'seq 0 "$clen"'; do
(echo "${commands[$i]}" | bash) & # run the command via bash in subshell
echo "$i ith command has been issued as a background job"
done
wait # wait for all to finish
echo "EXIT_CODE => $EXIT_CODE"
exit "$EXIT_CODE"