bash – 同时执行多个shell脚本

前端之家收集整理的这篇文章主要介绍了bash – 同时执行多个shell脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想做以下事情:

>同时执行多个shell脚本(此处为2个脚本).
>等到两个脚本完成
>转储每个脚本的返回值

但是,main.sh无法按预期工作.

main.sh

#!/bin/bash

ret1=`./a.sh` &
ret2=`./b.sh`

if [ "${ret1}"="" -a "${ret2}"="" ]; then
   sleep 1
else
   echo ${ret1},${ret2}
end

#!/bin/bash
sleep 10
echo 1

b.sh

#!/bin/bash
sleep 5
echo 2
这是我一直在运行的一些代码,它们似乎完全符合您的要求.只需在适当的位置插入./a.sh和./b.sh:
# Start the processes in parallel...
./script1.sh 1>/dev/null 2>&1 &
pid1=$!
./script2.sh 1>/dev/null 2>&1 &
pid2=$!
./script3.sh 1>/dev/null 2>&1 &
pid3=$!
./script4.sh 1>/dev/null 2>&1 &
pid4=$!

# Wait for processes to finish...
echo -ne "Commands sent... "
wait $pid1
err1=$?
wait $pid2
err2=$?
wait $pid3
err3=$?
wait $pid4
err4=$?

# Do something useful with the return codes...
if [ $err1 -eq 0 -a $err2 -eq 0 -a $err3 -eq 0 -a $err4 -eq 0 ]
then
    echo "pass"
else
    echo "fail"
fi

请注意,这会捕获脚本的退出状态,而不是它输出到stdout的内容.没有简单的方法来捕获在后台运行的脚本的标准输出,因此我建议您使用exit status将信息返回给调用进程.

原文链接:https://www.f2er.com/bash/387026.html

猜你在找的Bash相关文章