我有一个类似的脚本:
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
当其中一个后台命令foo,bar或baz失败时如何退出?
编辑:上述参考文献的解决方案:
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