BASH Programming

Bash Parallel Jobs Using For Loop

Many ways exist in Linux to run multiple commands or Bash scripts in parallel. One of the ways is to use a “for” loop in the Bash script to run a parallel job. This loop is very useful to run the parallel jobs using the “seq” command. The different ways of executing the parallel jobs using a “for” loop are shown in this tutorial.

Different Examples of Parallel Jobs

The different ways of running the parallel jobs using the “for” loop are shown in this part of the tutorial.

Example 1: Run the Parallel Jobs Using the “For” Loop

Using the “for” loop is the simpler way to perform the parallel jobs in the Bash script. Create a Bash file with the following script that runs the “for” loop 10000 times and print a number after the iteration of 1000 times. This task is done in parallel using the “for” loop.

#!/bin/bash

#Iterate the loop until reaches to 10000

for val in `seq 0 1000 10000`;

do

#Print every 1000th number

echo $val

done

The following output appears after executing the script. There are 10 numbers between 0 and 10000 that are printed in the output:

p3

Example 2: Run the Parallel Jobs Using the Nested “For” Loop

Create a Bash file with the following script that runs the nested “for” loop that generates the serial number using the alphabetic characters from “A” to “C” and the numbers 1 to 3. In the first iteration of the outer loop and after completing the iteration of the inner loop, “A1. CodeIgniter”, “A2. Laravel”, and “A3. CakePHP” are printed. In the second iteration of the outer loop and after completing the iteration of the inner loop, “B1. Oracle”, “B2. MySQL”, and “B3. SQL” are printed. In the third iteration of the outer loop and after completing the iteration of the inner loop, “C1. CSS”, “C2. JQuery”, and “C3. JavaScript” are printed.

#Outer loop

for alpha in {A..C}

do

#Inner loop

for number in {1..3}

do

#Print the output based on the condition

if [ $alpha == 'A' ]; then

arrayList=("CodeIgniter" "Laravel" "CakePHP")

elif [ $alpha == 'B' ]; then

arrayList=("Oracle" "MySQL" "SQL")

elif [ $alpha == 'C' ]; then

arrayList=("CSS" "JQuery" "JavaScript")

fi

echo "$alpha$number. ${arrayList[$number-1]}"

done

done

The following output appears after executing the script:

Example 3: Run the Parallel Jobs Using the “For” Loop and “Wait” Command

The “wait” command is a very useful command of Bash that is used to wait for one job to complete the task when multiple jobs are running. If fewer jobs are running, the “wait” command starts a new job asynchronously. Create a Bash file with the following script that runs a background job inside the nested “for” loop. The “wait” command is used for waiting to complete all the child processes. The “date” and “sleep” commands are executed as the background process.

#Outer loop

for i in {1..2}

do

#Inner loop

for j in {1..3}

do

if test "$(jobs | wc -l)" -ge 2; then

wait -n

fi

#Background process

{

date

sleep 1

} &

done

done

The following output appears after executing the script. The current date and time are printed 6 times from the background process to iterate the nested “for” loops for 2×3=6 times:

Example 4: Differences Between Sequential and Parallel Runs

Create a Bash file with the following script that shows the differences between the sequential run and the parallel run. The prn_char() function is defined in the script to print five characters with 0.5 second duration. Next, the first “for” loop is used to run the prn_char() function sequentially. The second “for” loop is used to run the prn_char() function in parallel.

#Define a function to print 5 characters with 0.5 second duration

prn_char(){

for c in h e l l o; do

sleep 0.5;

echo -n $c;

done

echo

}

#Run the function using for loop sequentially

for out in {1..3}; do

prn_char "$out"

done

#Run the function using for loop in parallel

for out in {1..3}; do

prn_char "$out" &

done


The following output appears after executing the script. The difference between the sequential run and the parallel run is shown in the output. Here, all the characters of the “for” loop of the prn_char() function are printed at a time in the sequential run and each character is printed three times in the parallel run:

p4

Conclusion

Running the parallel jobs using “for” loop is required for many programming purposes. The methods of running the parallel jobs using the “for” loop are shown in this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.