如何获取从一个初始启动脚本生成的多个 bash 后台脚本来登录终端的正确行?

2024-01-29

我有一个类似这样的 bash 脚本:

# one.sh, two.sh and three.sh do not depend on anything,
# and can run simultaneously in any order,
# and the execution time for each is random

# four.sh can only run once three.sh has finished

one.sh &
ONE=$!

two.sh &
TWO=$!

three.sh &
THREE=$!

wait $THREE
four.sh

其中 one.sh、two.sh 和 Three.sh 看起来像这样:

echo -n "doing stuff..."
#some command or set of commands
if [ $? ]
then
    echo $RESULT_PASS
else
    echo $RESULT_FAIL
fi

我得到的输出是这样的:

doing stuff1...done
doing stuff2...
doing stuff3...
doing stuff4...done
7263
doing stuff5...done
doing stuff6...9823
doing stuff7...done 
9283

因为一个脚本中的某些任务在另一脚本中的任务开始之前尚未完成。

我正在寻找的输出是这样的:

doing stuff1...done
doing stuff2...done
doing stuff3...8373
doing stuff4...done
doing stuff5...1234
doing stuff6...fail
doing stuff7...done

我将不胜感激一些指导。

Thanks!


我会用GNU 并行并行运行脚本。它非常简单、直观且灵活。因此,要并行运行三个脚本并保持输出顺序并等待所有三个脚本完成:

parallel -k ::: ./one.sh ./two.sh ./three.sh

样本输出

doing stuff1...pass
doing stuff2...pass
doing stuff3...pass

搜索示例堆栈溢出通过输入[gnu-parallel]在搜索框中 - 包含方括号。您可以并行运行任意数量的脚本、标记输出行、跨多个进程循环大文件、远程登录到其他计算机以将进程分布在网络上、如果一个脚本失败或 10% 失败则停止所有其他脚本,或者如果有任何通过的话...


或者你可以问GNU 并行用脚本名称标记每一行:

parallel -k --tag ::: ./one.sh ./two.sh ./three.sh
./one.sh    doing stuff1...pass
./two.sh    doing stuff2...pass
./three.sh  doing stuff3...pass
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取从一个初始启动脚本生成的多个 bash 后台脚本来登录终端的正确行? 的相关文章

随机推荐