2

I have a script something like this:

go build foo -o /tmp/foo \
&& echo 'Compressing foo' \
&& upx /tmp/foo \
|| exit 11 & 
#  ^ this exit didn't exit the caller script

go build bar -o /tmp/bar \
&& echo 'Compressing bar' \
&& upx /tmp/bar \
|| exit 12 & 

go build baz -o /tmp/baz \
&& echo 'Compressing baz' \
&& upx /tmp/baz \
|| exit 13 & 

wait

scp /tmp/foo /tmp/bar /tmp/baz someuser@someserver:/tmp

How to make it exit when one of the background command either foo, bar or baz failed?

EDIT: Solution from the reference above:

PIDS=''

go build foo -o /tmp/foo \
&& echo 'Compressing foo' \
&& upx /tmp/foo & 
PIDS="$PIDS $!"

go build bar -o /tmp/bar \
&& echo 'Compressing bar' \
&& upx /tmp/bar & 
PIDS="$PIDS $!"

go build baz -o /tmp/baz \
&& echo 'Compressing baz' \
&& upx /tmp/baz  & 
PIDS="$PIDS $!"

for pid in $PIDS; do
    wait $pid || let 'fail=1'
done

if [ "$fail" == '1' ]; then
    exit 11
fi

scp /tmp/foo /tmp/bar /tmp/baz someuser@someserver:/tmp
Kokizzu
  • 24,974
  • 37
  • 137
  • 233

0 Answers0