Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果任何子shell失败,Bash将完全失败_Bash_Shell - Fatal编程技术网

如果任何子shell失败,Bash将完全失败

如果任何子shell失败,Bash将完全失败,bash,shell,Bash,Shell,我想并行运行一些脚本,如果所有脚本都成功,我将执行一些额外的命令。但是,如果任何子shell失败,我希望立即退出 我想用一个例子来解释更容易: exit_in_1() { sleep 1 echo "This should be the last thing printed" exit 1 } exit_in_2() { sleep 2 echo "This should not be printed" exit 1 } exit_in_1 & exit_in

我想并行运行一些脚本,如果所有脚本都成功,我将执行一些额外的命令。但是,如果任何子shell失败,我希望立即退出

我想用一个例子来解释更容易:

exit_in_1() {
  sleep 1
  echo "This should be the last thing printed"
  exit 1
}

exit_in_2() {
  sleep 2
  echo "This should not be printed"
  exit 1
}

exit_in_1 &
exit_in_2 &

echo "This should be printed immediately"

sleep 3

echo "This should not be printed also"

上面的脚本打印了所有的回音。

感谢@Mansuro的评论,我这样解决了我的问题:

exit_later() {
  sleep $1
  echo "This should be printed twice"
  exit $(($1-1))
}

pids=""
for i in {1..10}; do
  exit_later $i &
  pids+=" $!"
done

echo "This should be printed immediately"

for p in $pids; do
  if ! wait $p; then
    kill $pids
    exit 1
  fi
done

echo "This should not be printed"

我不认为这个问题是重复的。是的,其他问题已经有了答案,但问题不同。我不知道我需要检查后台进程的退出代码才能使主进程失败。像我这样的人都能从这个问题中受益。请注意,使问题重复的是问题本身而不是答案。