Shell脚本运行多个文件 [英] shell script run multiple files

查看:1028
本文介绍了Shell脚本运行多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在并行运行100个文件的for循环中使用shell脚本.

I want to use a shell scrip in a for-loop that run 100 files in parallel.

当前,我具有以下格式的shell脚本:

Currently, I have a shell script of the following format:

#!/bin/bash
NUM=10
python a1.py $((NUM + 0)) &
python a2.py $((NUM + 2)) &
python a3.py $((NUM + 4)) &
python a4.py $((NUM + 6)) &
python a5.py $((NUM + 8)) &

现在,如果我有a1.pya2.pya3.py .... a100.py,并且我想并行运行它们,该如何在for循环中进行?

Now, if I have a1.py, a2.py, a3.py .... a100.py, and I want to run them in parallel, how do I do it in for-loop?

推荐答案

如果您具有bash版本4并运行此程序:

If you have bash version 4 and run this:

echo {10..208..2} 

您将获得:

10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60
62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 
110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 
148 150 152 154 156 158 160 162 164 166 168 170 172 174 176 178 180 182 184 
186 188 190 192 194 196 198 200 202 204 206 208

看起来像您的系列.然后,如果您想并行运行许多作业,则可以使用 GNU Parallel .这为您提供{#}作为职位编号的占位符.因此,如果运行此命令:

which looks like your series. Then, if you want to run lots of jobs in parallel, I would use GNU Parallel. That offers you {#} as a placeholder for the job number. So, if you run this:

parallel -k echo {#} {} ::: {10..208..2}

您将获得:

1 10
2 12
3 14
4 16
5 18

因此,要运行实际的脚本,例如:

So, to run your actual scripts, something like:

parallel -k --dry-run 'python a{#}.py {}' ::: {10..208..2}

示例输出

python a1.py 10
python a2.py 12
python a3.py 14
python a4.py 16
...
...

如果看起来不错,请在没有--dry-run且没有-k的情况下再次运行,该命令会保留输出以便于调试.

If that looks good, run again without the --dry-run and without the -k which keeps the output in order to make it easier to debug.

TLDR;

对于 GNU Parallel ,我最简洁的回答是:

My most concise answer, with GNU Parallel is:

parallel python a{#}.py {} ::: {10..208..2}

或者如果您没有bash版本4:

Or if you don't have bash version 4:

parallel python a{#}.py {} ::: $(seq 10 2 208)

这篇关于Shell脚本运行多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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