Question

I want to run a command with different arguments in multi-threading form,

What I tried is:

#!/bin/bash

ARG1=$1
ARG2=$2
ARG3=$3

for ...  #counter is i
do
 main command with ARG1 ARG2 ARG3  & a[i]=$!
done
wait `echo  ${a[@]}`

I used & a[i]=$! in for loop and wait $(echo ${a[@]}) after for loop. I want my bash to wail till all threads finish then echo their pid for me...

But when I run my script after some time it waits.

Thank you

Était-ce utile?

La solution

I think you want this:

#!/bin/bash
for i in 0 1 2
do
   sleep 3 & a[$i]=$!
done
wait
echo ${a[@]}

You are missing the $ on the array index $i in your script. Also, you don't need to say which PIDs you are wating for if you are waiting for all of them. And you also said you wanted to see the list of PIDs at the end.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top