如何在Shell脚本中使用并行执行? [英] How to use parallel execution in a shell script?

查看:1744
本文介绍了如何在Shell脚本中使用并行执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C shell脚本,它执行以下操作:

I have a C shell script that does something like this:

#!/bin/csh
gcc example.c -o ex
gcc combine.c -o combine
ex file1 r1     <-- 1
ex file2 r2     <-- 2
ex file3 r3     <-- 3
#... many more like the above
combine r1 r2 r3 final
\rm r1 r2 r3

是否可以通过某种方式使行123平行中运行,而不是一个接一个地运行?

Is there some way I can make lines 1, 2 and 3 run in parallel instead of one after the another?

推荐答案

将其转换为具有适当依赖性的Makefile.然后,您可以使用make -j让Make并行运行一切.

Convert this into a Makefile with proper dependencies. Then you can use make -j to have Make run everything possible in parallel.

请注意,Makefile中的所有缩进必须 是TAB. TAB显示生成"命令的运行位置.

Note that all the indents in a Makefile must be TABs. TAB shows Make where the commands to run are.

还请注意,此Makefile现在正在使用GNU Make扩展名(通配符和subst函数).

Also note that this Makefile is now using GNU Make extensions (the wildcard and subst functions).

它可能看起来像这样:

export PATH := .:${PATH}

FILES=$(wildcard file*)
RFILES=$(subst file,r,${FILES})

final: combine ${RFILES}
    combine ${RFILES} final
    rm ${RFILES}

ex: example.c

combine: combine.c

r%: file% ex
    ex $< $@

这篇关于如何在Shell脚本中使用并行执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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