Sunchi
Sunchi

Reputation: 95

How to send arguments to bash script in GNU parallel

I want to use GNU parallel to execute some commands in parallel, but I don't know how to send the argument to my bash scripts.

My bash_script.sh:

scp $1.zip [email protected]:~/
scp $1.zip [email protected]:~/
scp $1.zip [email protected]:~/
scp $1.zip [email protected]:~/
scp $1.zip [email protected]:~/

I can send the argument to the bash_script and execute it in sequence.

bash bash_script.sh argument

but how to do it in parallel?

parallel -a bash_script argument

Upvotes: 2

Views: 1928

Answers (4)

Sunchi
Sunchi

Reputation: 95

bash_script.sh

parallel scp "$1" xxx@{}.com: ::: {1..5}

Usage:

bash bash_script.sh argument

Upvotes: 0

tripleee
tripleee

Reputation: 189357

parallel can execute individual processes in parallel, but a script is usually intended to be run as a single process.

You could do

parallel scp "$1" xxx@{}1.xyz.com: ::: {1..5}

or if you really wanted to split the script file into individual lines

sed "s#\$1#$1#g" bash_script.sh | parallel

If the script was just static text, you could simply have parallel read it line by line; but parallel doesn't have a value for $1 in that context, so we substitute it in, and then piped the substituted lines for parallel execution.

Tangentially, notice proper variable quoting and don't put a .sh extension on a Bash script. (Don't put any extension on any script, basically.)

Upvotes: 1

Gonzalo Matheu
Gonzalo Matheu

Reputation: 10064

The solution that worked for me is writing the commands to a text file (e.g: commands.txt) and the executing parallel:

parallel -j 2 < commands.txt

Upvotes: 1

ShawnMilo
ShawnMilo

Reputation: 6195

Maybe something like this:

for server in server1 server2 server 3
do
   scp "$1" $server: &
done

The & causes the job to run in the background. If I'm not understanding your requirements, please clarify.

Upvotes: 0

Related Questions