使用文件输入运行并行命令的Bash脚本

时间:2021-08-10 00:09:21

I am trying to make a shell script which reads a configuration file and executes commands for each line in parallel.

我正在尝试创建一个shell脚本,该脚本读取配置文件并并行地执行每一行的命令。

For example, I have IPs.cfg, which can contain a variable amount of IPs. It could be one or several

例如,我有IPs。cfg可以包含可变数量的IPs。它可以是一个或几个

IPs.cfg

IPs.cfg

145.x.x.x
176.x.x.x
192.x.x.x

I want to read the file and then execute a command for each line at the same time... for instance :

我想读取文件,然后同时为每一行执行一个命令……例如:

scp test.iso root@$IP1:/tmp &
scp test.iso root@$IP2:/tmp &
scp test.iso root@$IP3:/tmp &
wait

The way I'm thinking this is that I store the IPs into an array

我的想法是把ip存储到数组中

IFS=$'\n' read -d '' -r -a array < IPs.cfg

Then I extract the number of lines from the file and decrease it by 1 since the array starts at 0.

然后我从文件中提取行数并将其减少1,因为数组从0开始。

NUMLINES=`cat IPs.cfg | wc -l`
NUMLINES=$((NUMLINES-1))

Now I want to execute the commands all at the same time. It's a variable number of parameters so I can't just manually use scp test.iso root@${array[0]}:/tmp & scp test.iso root@${array[1]}:/tmp & so on. I could use a while loop, but that would mean doing the commands one at a time. I'm also thinking about using recursion, but I have never done that in a bash script.

现在我想同时执行这些命令。这是一个可变数量的参数,所以我不能手动使用scp测试。iso root@${array[0]}:/tmp & scp测试。iso root@${array[1]}:/tmp等。我可以使用while循环,但这意味着每次只执行一个命令。我也在考虑使用递归,但我从来没有在bash脚本中这样做过。

It might be a silly question, but what are my options here?

这可能是个愚蠢的问题,但我的选择是什么呢?

3 个解决方案

#1


1  

The loop should look like this:

循环应该是这样的:

while read -r ip ; do
   scp test.iso "root@$ip:/tmp" &
done < IPs.conf
wait

#2


2  

You could make use of GNU parallel

您可以使用GNU并行

#3


0  

with this trick, you can control the number of simultaneous processes running at the same:

有了这个技巧,您就可以控制同时运行的进程的数量:

cat IPs.conf | xargs -n1 -I{} -P10 scp test.iso "root@{}:/tmp"

Check the -p10 which means to use 10 processes at the same time.

检查-p10,这意味着同时使用10个进程。

#1


1  

The loop should look like this:

循环应该是这样的:

while read -r ip ; do
   scp test.iso "root@$ip:/tmp" &
done < IPs.conf
wait

#2


2  

You could make use of GNU parallel

您可以使用GNU并行

#3


0  

with this trick, you can control the number of simultaneous processes running at the same:

有了这个技巧,您就可以控制同时运行的进程的数量:

cat IPs.conf | xargs -n1 -I{} -P10 scp test.iso "root@{}:/tmp"

Check the -p10 which means to use 10 processes at the same time.

检查-p10,这意味着同时使用10个进程。