How to wait in bash for several subprocesses to finish and return exit code !=0 when any subprocess ends with code !=0?

后端 未结 30 2858
悲哀的现实
悲哀的现实 2020-11-22 03:50

How to wait in a bash script for several subprocesses spawned from that script to finish and return exit code !=0 when any of the subprocesses ends with code !=0 ?

S

30条回答
  •  无人共我
    2020-11-22 04:22

    set -e
    fail () {
        touch .failure
    }
    expect () {
        wait
        if [ -f .failure ]; then
            rm -f .failure
            exit 1
        fi
    }
    
    sleep 2 || fail &
    sleep 2 && false || fail &
    sleep 2 || fail
    expect
    

    The set -e at top makes your script stop on failure.

    expect will return 1 if any subjob failed.

提交回复
热议问题