使用set -e在bash中等待多个进程 [英] Waiting for multiple processes in bash with set -e

查看:37
本文介绍了使用set -e在bash中等待多个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,我想在该脚本中并行运行两个进程,并且如果两个进程中的任何一个返回非零值,脚本都会失败.我最初尝试的一个最小示例是:

I have a bash script where I would like to run two processes in parallel, and have the script fail if either of the processes return non-zero. A minimal example of my initial attempt is:

#!/bin/bash

set -e
(sleep 3 ; true ) &
(sleep 4 ; false ) &
wait %1 && wait %2
echo "Still here, exit code: $?"

按预期,由于 wait%1&&等待%2 失败,并且由于 set -e 而导致脚本退出.但是,如果将 wait 反转,以使第一个具有非零状态( wait%2&& wait%1 ),则消息已打印:

As expected this doesn't print the message because wait %1 && wait %2 fails and the script exits due to the set -e. However, if the waits are reversed such that the first one has the non-zero status (wait %2 && wait %1), the message is printed:

$ bash wait_test.sh
Still here, exit code: 1

将每个 wait 放在自己的行上,并按我的意愿进行工作,如果两个进程中的任何一个失败,则退出脚本,但事实是,该脚本不适用于&&让我怀疑我在这里误会了一些东西.

Putting each wait on its own line works as I want and exits the script if either of the processes fail, but the fact that it doesn't work with && makes me suspect that I'm misunderstanding something here.

任何人都可以解释发生了什么事吗?

Can anyone explain what's going on?

推荐答案

bash 手册部分中,有关 set

-e 如果管道(可能由一个简单命令组成),列表或复合命令(请参见上面的SHELL GRAMMAR)以非零状态退出,则立即退出.如果失败的命令是紧随一段时间或直到关键字之后的命令列表的一部分,紧随if或elif保留字之后的测试的一部分,以及在&&中执行的任何命令的部分的一部分,则外壳程序不会退出.或||列表,除了最后一个&&之后的命令或|| ,管道中除最后一条命令外的任何命令,或者该命令的返回值用!反转.如果除子Shell之外的复合命令由于在-e被忽略时命令失败而返回非零状态,则该Shell不会退出.在退出外壳程序之前,将执行ERR上的陷阱(如果已设置).此选项分别适用于Shell环境和每个子Shell环境(请参见上面的COMMAND EXECUTION ENVIRONMENT),并且可能导致子Shell在执行子Shell中的所有命令之前退出.

-e Exit immediately if a pipeline (which may consist of a single simple command), a list, or a compound command (see SHELL GRAMMAR above), exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command's return value is being inverted with !. If a compound command other than a subshell returns a non- zero status because a command failed while -e was being ignored, the shell does not exit. A trap on ERR, if set, is executed before the shell exits. This option applies to the shell environment and each subshell environment separately (see COMMAND EXECUTION ENVIRONMENT above), and may cause subshells to exit before executing all the commands in the subshell.

tl;博士

在bash脚本中,用于这样的命令列表

In a bash script, for a command list like this

command1 && command2

command1 在单独的环境中运行,因此它不会影响脚本的执行环境.但是 command2 在当前环境中运行,因此可能会影响

command1 is run in a separate environment, so it cannot affect the script's execution environment. but command2 is run in the current environment, so it can affect

这篇关于使用set -e在bash中等待多个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆