巴什和放大器; (符号)运算符 [英] Bash & (ampersand) operator

查看:114
本文介绍了巴什和放大器; (符号)运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在bash shell中并行运行3个命令:

I'm trying to run 3 commands in parallel in bash shell:

$ (first command) & (second command) & (third command) & wait

这样做的问题是,如果第一个命令失败,例如,出口code是 0 (我猜是因为成功)。

The problem with this is that if first command fails, for example, the exit code is 0 (I guess because wait succeeds).

期望的行为是,如果命令之一出现故障,则出射code将是非零(并且理想地,在其他运行指令将被停止)。

The desired behavior is that if one of the commands fails, the exit code will be non-zero (and ideally, the other running commands will be stopped).

我怎么能做到这一点?

请注意,我想以并行方式运行的命令!

Please note that I want to run the commands in parallel!

推荐答案

我能想到的最好的是:

first & p1=$!
second & p2=$!
...

wait $p1 && wait $p2 && ..

wait $p1 || ( kill $p2 $p3 && exit 1 )
...

然而,这仍然强制执行的过程中检查的顺序,因此如果第三立即失败,你不会注意到它,直到第一和第二饰面。

however this still enforces an order for the check of processes, so if the third fails immediately you won't notice it until the first and second finishes.

这篇关于巴什和放大器; (符号)运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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