Loop understanding for performing same task multiple times easily


Last Blog Review →
In the last blog we understood, simple shell scripts to use if-else to get the work done.
Why Loops are required ?
Used to execute a cmd or a set of cmds many times
To iterate through files
To iterate lines within a file
Iterate through o/p of a cmd
Syntax For Loops
- First way → Providing the list of commands in the first line of for loop
for missions in <list of missions>
do
....
done
1st way to read data in for loop
for missions in A_mission B_mission C_mission
do
$missions
done
When the for loop runs for 1st time -> $missions variable is assigned A_mission, when for loop runs for 2nd time -> $missions variable is B_mission and so on.
Issue/Disadvantage of this type -
But there is one catch say if there are many names in the for loop say 100, we can't write the 100 names in the 1st line of for loop.
- Second way → Reading a file in the first line of for loop
To solve this issue mentioned above we can store the 100 names in a separate file and read the data from that separate file in the for loop. And that can be done using "cat" command in ' ' quotes. So, the for loop will first read all the data from another file and then execute the for loop. Lets understand.
for aplhabets in $(cat Alphabet.txt)
do
$alphabets
done
Here the Alphabet.txt has 26 letters which are used in for loop. We can write them one by one in 1st line of for loop but will be too hectic. So, we have created a separate file of alphabet.txt which has all 26 letters & in the for loop we have used 'cat alphabet.txt' which means when the for loop will be executed then the first line of for loop executes where the $(cat alphabet.txt) will run first and read all the data from the separate file. then for loop will start executing its operation.
Say if we want to add few more data in the separate file i.e. Alphabet.txt file then we can add and then the for loop will execute those as well. So, we don't need to change the for loop everytime even if information is changed. The reason for using "$( )" for the cat command is that it's easier to read and has advantage when you plan to embed multiple commands i.e. commands within commands.
Third way → Another way of printing the values in a range using for loop following way
for num in {0..100} do num done
for (( num = 1; num <= 100; num++ )) do num done
It’s a simple printing range of number’s which is also supported.
Conclusion →
In the this blog we understood, why for loop is required and what are the multiple ways we can execute the for loop to be used in shell scripting.
Image Credit →
Subscribe to my newsletter
Read articles from Mihir Suratwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Mihir Suratwala
Mihir Suratwala
Hi, How are you !! Hope you doing good.... I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective. So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.