bash shell 脚本,for 循环,是否循环等待执行命令进行迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30663621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
shell script, for loop, does loop wait for execution of the command to iterate
提问by Ankit Kumar Singhal
I have a shell script with a for loop. Does loop wait for execution of the command in its body before iterating?
我有一个带有 for 循环的 shell 脚本。循环在迭代之前是否等待执行其主体中的命令?
Thanks in Advance
提前致谢
Here is my code. Will the commands execute sequentially or parallel?
这是我的代码。命令是顺序执行还是并行执行?
for m in "${mode[@]}"
do
cmd="exec $perlExecutablePath $perlScriptFilePath --owner $j -rel $i -m $m"
$cmd
eval "$cmd"
done
采纳答案by Tom Fenech
Assuming that you haven't background-ed the command, then yes.
假设您还没有对命令进行后台处理,那么是的。
For example:
例如:
for i in {1..10}; do cmd; done
waits for cmd
to complete before continuing the loop, whereas:
cmd
在继续循环之前等待完成,而:
for i in {1..10}; do cmd &; done
doesn't.
没有。
If you want to run your commands in parallel, I would suggest changing your loop to something like this:
如果您想并行运行您的命令,我建议您将循环更改为如下所示:
for m in "${mode[@]}"
do
"$perlExecutablePath" "$perlScriptFilePath" --owner "$j" -rel "$i" -m "$m" &
done
This runs each command in the background, so it doesn't wait for one command to finish before the next one starts.
这会在后台运行每个命令,因此它不会在下一个命令开始之前等待一个命令完成。
An alternative would be to look at GNU Parallel, which is designed for this purpose.
另一种方法是查看专为此目的而设计的GNU Parallel。
回答by Ole Tange
Using GNU Parallel it looks like this:
使用 GNU Parallel 它看起来像这样:
parallel $perlExecutablePath $perlScriptFilePath --owner $j -rel $i -m {} ::: "${mode[@]}"
GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the same machine or on multiple machines you have ssh access to. It can often replace a for
loop.
GNU Parallel 是一个通用的并行器,可以很容易地在同一台机器或您可以 ssh 访问的多台机器上并行运行作业。它通常可以代替for
循环。
If you have 32 different jobs you want to run on 4 CPUs, a straight forward way to parallelize is to run 8 jobs on each CPU:
如果您有 32 个不同的作业要在 4 个 CPU 上运行,一个直接的并行化方法是在每个 CPU 上运行 8 个作业:
GNU Parallel instead spawns a new process when one finishes - keeping the CPUs active and thus saving time:
GNU Parallel 会在完成后生成一个新进程 - 保持 CPU 处于活动状态,从而节省时间:
Installation
安装
If GNU Parallel is not packaged for your distribution, you can do a personal installation, which does not require root access. It can be done in 10 seconds by doing this:
如果没有为您的发行版打包 GNU Parallel,您可以进行个人安装,这不需要 root 访问权限。这样做可以在 10 秒内完成:
(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash
For other installation options see http://git.savannah.gnu.org/cgit/parallel.git/tree/README
对于其他安装选项,请参阅http://git.savannah.gnu.org/cgit/parallel.git/tree/README
Learn more
了解更多
See more examples: http://www.gnu.org/software/parallel/man.html
查看更多示例:http: //www.gnu.org/software/parallel/man.html
Watch the intro videos: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
观看介绍视频:https: //www.youtube.com/playlist?list =PL284C9FF2488BC6D1
Walk through the tutorial: http://www.gnu.org/software/parallel/parallel_tutorial.html
演练教程:http: //www.gnu.org/software/parallel/parallel_tutorial.html
Sign up for the email list to get support: https://lists.gnu.org/mailman/listinfo/parallel
注册电子邮件列表以获得支持:https: //lists.gnu.org/mailman/listinfo/parallel