执行多个shell脚本同时

编程入门 行业动态 更新时间:2024-10-26 20:29:18
本文介绍了执行多个shell脚本同时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要做以下几件事:搜索结果

  • 执行多个shell脚本(这里是2脚本)兼任。

  • 等待两个脚本完成

  • 转储每个脚本的返回值

然而, main.sh 不能按预期工作。

结果

main.sh

#!/斌/庆典RET1 =`/ a.sh`和放大器。RET2 =`/ b.sh`如果[$ {} RET1=-a$ {} RET2=];然后   睡眠1其他   回声$ {} RET1,$ {} RET2结束

a.sh

#!/斌/庆典睡眠10回音1

b.sh

#!/斌/庆典睡眠5回音2

解决方案

如果您有GNU并行 HTTP://www.gnu .ORG /软件/并口/ 安装,你可以这样做:

平行-j0'{};回声$? ::: a.sh b.sh

我要退出code,检查其中的一个失败的怀疑,那你其实并不关心什么precise退出code了。在这种情况下,你可以这样做:

平行-j0 ::: a.sh b.sh ||呼应一方或双方的失败

如果它足以获得失败的最后的误差code:

平行-j0 --halt 1 ::: a.sh b.sh;回声$?

也许你想干掉a.sh如果b.sh年初完成,但失败:

平行-j0 --halt 2 ::: a.sh b.sh;回声$?

您可以安装GNU并行只需:

wget的git.savannah.gnu/cgit/parallel.git/plain/src/parallel行chmod 755并行CP并行SEM

留意GNU并行,以了解更多的介绍视频:www.youtube/playlist?list=PL284C9FF2488BC6D1

I want to do the following things:

  • Execute multiple shell scripts (here 2 scripts) concurrently.

  • Wait until both scripts finish

  • Dump return value of each script

However, main.sh does not work as expected.

main.sh

#!/bin/bash ret1=`./a.sh` & ret2=`./b.sh` if [ "${ret1}"="" -a "${ret2}"="" ]; then sleep 1 else echo ${ret1},${ret2} end

a.sh

#!/bin/bash sleep 10 echo 1

b.sh

#!/bin/bash sleep 5 echo 2

解决方案

If you have GNU Parallel www.gnu/software/parallel/ installed you can do this:

parallel -j0 '{}; echo $?' ::: a.sh b.sh

I have a suspicion that you want the exit code to check if one of them failed, and that you actually do not care what the precise exit code was. In that case you can do:

parallel -j0 ::: a.sh b.sh || echo one or both of them failed

If it is sufficient to get the error code of the last that failed:

parallel -j0 --halt 1 ::: a.sh b.sh; echo $?

Maybe you would like to kill a.sh if b.sh finishes early but fails:

parallel -j0 --halt 2 ::: a.sh b.sh; echo $?

You can install GNU Parallel simply by:

wget git.savannah.gnu/cgit/parallel.git/plain/src/parallel chmod 755 parallel cp parallel sem

Watch the intro videos for GNU Parallel to learn more: www.youtube/playlist?list=PL284C9FF2488BC6D1

更多推荐

执行多个shell脚本同时

本文发布于:2023-06-06 09:44:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/539009.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   脚本   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!