ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

Shell Script Chapter 6: Shell Script Looping Statements

prodevelopertutorial February 13, 2019

If there is a need to execute set of statements repeatedly, then we need to use looping statements. Below is the list of statements supported by shell script.

6.1. While Loop
6.2. Until Loop
6.3. For Loop
6.4. Select Loop

6.1. While Loop

While loop is a looping statement that is used to execute a set of statement until the condition is true. Once the condition becomes false, it will exit out of the loop. Below is the syntax for while loop.

Syntax:

while [ condition ]
do
#statements

done

Note:

Here we use “while”, “do” and “done” keywords used in while loop.

Example:

#!/bin/bash

var=5

while [ $var -ge 1 ]
do
echo "the number is $var"
var=$(($var - 1))
done

Output:

the number is 5
the number is 4
the number is 3
the number is 2
the number is 1

 

6.2. Until Loop

Until loop is similar to wile loop with a twist. Here the loop will continue to execute till the expression becomes true. As long as the condition fails, the loop continues.

Syntax:

until [ condition ]
do
#statements
done

 

Note: Here we use “until”, “do” and “done” keywords used in until loop.

Example:

#!/bin/bash

var=1

until [ $var -eq 5 ]
do
echo "the number is $var"
var=$(( $var + 1 ))
done

Output:

the number is 1
the number is 2
the number is 3
the number is 4

As you see in above output, when the “var” value becomes 5, it will exit the loop. Hence it will not print the number 5.

6.3. For Loop

For loop is similar to while and until loop, but the number of times to loop should be known beforehand. There are 2 kinds of for loop.

1. The bash form of for loop:

Syntax:

for <variable_name> in <list>
do
#statements
done

 

Note:

Here “for”, “in”, “do”, “done” are the keyword.
list can be a list of numbers, array list, or any other that has a list of items.
<variable_name> holds the list of items, one element at a time.

Example:

#!/bin/bash

for var in 2 4 6 8 10
do
echo "The variable is $var"
done

Output:

The variable is 2
The variable is 4
The variable is 6
The variable is 8
The variable is 10

 

2. The “c” style of for loop:

for (( <initialization> ; <condition>; <increment_or_decrement> ))
do
#statements
done

 

Here “for”, “;”, “do”, “done” are the keywords used.
<initialization> is used to initialize the variable
<condition> Here condition to be checked should be written. The statements between “do” and “done” will be executed until the condition become false.
<increment_or_decrement> After every loop, this expression will be executed.

Example:

#!/bin/bash

for (( var=0 ; var<=5; var++ ))
do
echo "The variable is $var"
done

Output:

The variable is 0
The variable is 1
The variable is 2
The variable is 3
The variable is 4
The variable is 5

 

6.4. Select Loop

Usually we use select loop, when we want to display the options to the user. Generally, select loop will be used with case or else-if statements for better control. As you can see from below syntax, there is no way to exit the select loop, break statement to be used to exit out of select loop.

Syntax:

select <variable_name> in <list>
do
#statements
done

Note:

“select” “in” “do” “done” are the keywords used.
The elements in “list” will be displayed to the user as a menu.

Example:

#!/bin/bash

echo "Select an operating system from below"

select var in linux unix macOS windows
do
echo "The OS you have selected is $var"
done

Output:

Select an operating system from below
1) linux
2) unix
3) macOS
4) windows
#? 3
The OS you have selected is macOS

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2022 ProDeveloperTutorial.com