0% found this document useful (0 votes)
197 views

Control Structure in Shell Script

The document discusses different types of shell control statements in Bash including sequential execution, conditional execution with if/else statements, and looping constructs with for, while, until, and case statements. It provides syntax examples and use cases for each type of control statement to demonstrate how they can be used in Bash scripting.

Uploaded by

Satyajeet Gaur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
197 views

Control Structure in Shell Script

The document discusses different types of shell control statements in Bash including sequential execution, conditional execution with if/else statements, and looping constructs with for, while, until, and case statements. It provides syntax examples and use cases for each type of control statement to demonstrate how they can be used in Bash scripting.

Uploaded by

Satyajeet Gaur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Shell Control Statements

Shell Control Statements


There are three components that we need to
understand in any structured programming
methodology:
• Sequential execution
• Conditional execution
• Looping constructs
Sequential Execution

• Sequential execution means that each command in a


program script executes in the order in which it is listed
in the program. The first command in the sequence
executes first and when it is complete, the second
command executes, and so on. The presence of functions
in the code does not negate sequential execution; we
can still follow the sequential flow of the instructions. As
technology advances there is more parallel programming
being run on smaller computers, but how that impacts
sequential execution is beyond the scope of this course.
Conditional Execution
• Conditional Statements: There are a total of
five conditional statements which can be used
in bash programming.
• if statement
• if-else statement
• if..elif..else..fi statement (Else If ladder)
• switch statement
if statement
• This block will process if specified condition is
true.
Syntax:
if [ expression ]
then
statement
fi
if-else statement
• If specified condition is not true in if part then else
part will be executed.
• Syntax:
if [ expression ]
then
statement1
else
statement2
fi
An example of the code:
#Initializing two variables
a=20
b=20

if (($a == $b ))
then
#If they are equal then print this
echo "a is equal to b"
else
#else print this
echo "a is not equal to b"
fi
if..elif..else..fi statement (Else If ladder)
• To use multiple conditions in one if-else block,
then the elif keyword is used in shell. If
expression1 is true then it executes statement
1 and 2, and this process continues. If none of
the conditions is true then it processes else
part.
Syntax:
if [ expression1 ]
then
statement1
statement2
.
.
elif [ expression2 ]
then
statement3
statement4
.
.
else
statement5
fi
• # if condition is true
• if [ "myfile" == "myfile" ];
• then
• echo "true condition"
• fi

• # if condition is false
• if [ "myfile" == "yourfile" ];
• then
• echo "false condition"
• fi
In this example, we demonstrate the usage of if
statement with a simple scenario of comparing two
strings:
Nested If Else
• Just like nested if statement, the if-else
statement can also be used inside another if-
else statement. It is called nested if-else in
Bash scripting.
• Following is an example explaining how to
make use of the nested if-else statement in
Bash:
• read -p "Enter a value:" value
• if [ $value -gt 9 ];
• then
• if [ $value -lt 11 ];
• then
• echo "$value>9, $value<11"
• else
• echo "The value you typed is greater than 9."
• fi
• else echo "The value you typed is not greater than 9."
• fi
• #!/bin/bash
• read -p "Enter a number of quantity:" num
• if [ $num -gt 100 ];
• then
• echo "Eligible for 10% discount"
• elif [ $num -lt 100 ];
• then
• echo "Eligible for 5% discount"
• else
• echo "Lucky Draw Winner"
• echo "Eligible to get the item for free"
• fi
• #!/bin/bash
• read -p "Enter a number of quantity:" num
• if [ $num -gt 200 ];
• then
• echo "Eligible for 20% discount"

• elif [[ $num == 200 || $num == 100 ]];
• then
• echo "Lucky Draw Winner"
• echo "Eligible to get the item for free"

• elif [[ $num -gt 100 && $num -lt 200 ]];


• then
• echo "Eligible for 10% discount"

• elif [ $num -lt 100 ];
• then
• echo "No discount"
• fi
 Case
• In this topic, we will discuss the basics of case statements and how to
use them in Bash scripts.
• The Bash case statement is the simplest form of IF-THEN-ELSE with
many ELIF elements. Using the case statement makes our bash script
more readable and easier to maintain. These are generally applied to
simplify the complex conditions having multiple different choices.
• The Bash case statement follows a similar logic as the Javascript or C
switch statement. There is a slight difference, as follows:
• The Bash case statement takes a value once and tests that value
multiple times. It stops searching for a pattern once it has found it
and executed the statement linked with it, which is almost opposite
in case of the C switch statement.
Case Statement Syntax
• Syntax of the bash case statement is given below:
• case expression in
• pattern_1)
• statements
• ;;
• pattern_2)
• statements
• ;;
• pattern_3|pattern_4|pattern_5)
• statements
• ;;
• pattern-n)
• statements
• ;;
• *)
• statements
• ;;
• esac
• #!/bin/bash

• echo "Do you know Java Programming?"
• read -p "Yes/No? :" Answer
• case $Answer in
• Yes|yes|y|Y)
• echo "That's amazing."
• echo
• ;;
• No|no|N|n)
• echo "It's easy. Let's start learning from javatpoint."
• ;;
• esac
• #!/bin/bash

• echo "Which Operating System are you using?"
• echo "Windows, Android, Chrome, Linux, Others?"
• read -p "Type your OS Name:" OS

• case $OS in
• Windows|windows)
• echo "That's common. You should try something new."
• echo
• ;;
• Android|android)
• echo "This is my favorite. It has lots of applications."
• echo
• ;;
• Chrome|chrome)
• echo "Cool!!! It's for pro users. Amazing Choice."
• echo
• ;;
• Linux|linux)
• echo "You might be serious about security!!"
• echo
• ;;
• *)
• echo "Sounds interesting. I will try that."
• echo
• ;;
• esac
For Loop
• In this topic, we will understand the usage of for
loop in Bash scripts.
• Like any other programming language, bash shell
scripting also supports 'for loops' to perform
repetitive tasks. It helps us to iterate a particular set
of statements over a series of words in a string, or
elements in an array. For example, you can either
run LINUX command (or task) many times or just
read and process the list of commands using a 'for
loop'.
Syntax of For Loop

• We can apply 'for loop' on bash script in two


ways. One way is 'for-in' and another way is
the c-style syntax. Following is the syntax of
'for loop' in bash shell scripting:
• for variable in list  
• do  
• commands  
• done  
• for (( expression1; expression2; expression3 ))
• do
• commands
• done
• #!/bin/bash
• #This is the basic example of 'for loop'.

• learn="Start learning from Javatpoint."

• for val in $learn
• do
• echo $val
• done

• echo "Thank You."
• #!/bin/bash  
• #This is the basic example to print a series of numbe
rs from 1 to 10.  
• for num in {1..10}  
• do  
• echo $num  
• done  
•   
• echo "Series of numbers from 1 to 10."  
• #!/bin/bash   
• #For Loop to Read a Range with Increment  
•   
• for num in {1..10..1}  
• do  
• echo $num  
• done  
For Decreament
• #!/bin/bash  
•   
• #For Loop to Read a Range with Decrement  
•   
• for num in {10..0..1}  
• do  
• echo $num  
• done  
For Loop to Read Three-expression
• Three expression syntax is the most common syntax of 'for loop'. The
first expression refers to the process of initialization, the second
expression refers to the termination, and the third expression refers
to the increment or decrement.
• Check out the example below to print 1 to 10 numbers using three
expressions with for loop:
• Bash Script
• #!/bin/bash  
• #For Loop to Read Three-expression  
•   
• for ((i=1; i<=10; i++))  
• do  
• echo "$i"  
• done  
• #!/bin/bash  
• #Table of 2  
•   
• for table in {2..100..2}  
• do  
• echo $table  
• if [ $table == 20 ]; then  
• break  
• fi  
• done  
• #!/bin/bash  
• #Numbers from 1 to 20, ignoring from 6 to 15
•  using continue statement"  
•   
• for ((i=1; i<=20; i++));  
• do  
• if [[ $i -gt 5 && $i -lt 16 ]];  
• then  
• continue  
• fi  
• echo $i  
• done  
Syntax of Bash While Loop
• Bash while loop has the following format:
• while [ expression ];  
• do  
• commands;  
• multiple commands;  
• done  
• If there are multiple conditions to include in the
expression, then the syntax of the while loop will
be as follows:
• while [ expressions ];  
• do  
• commands;  
• multiple commands;  
• done  
• The while loop one-liner syntax can be defined as:
• while [ condition ]; do commands; done  
• while control-command; do Commands; done  
• #!/bin/bash  
• #Script to get specified numbers  
•   
• read -p "Enter starting number: " snum  
• read -p "Enter ending number: " enum  
•   
• while [[ $snum -le $enum ]];  
• do  
• echo $snum  
• ((snum++))  
• done  
•   
• echo "This is the sequence that you wanted."  
While Loop with C-Style
• We can also write while loop in bash script as similar as a
while loop in C programming language.
• #!/bin/bash  
• #While loop example in C style  
• i=1  
• while((i <= 10))  
• do  
• echo $i  
• let i++  
• done  
• The syntax of until loop looks almost similar to the
syntax of bash while loop. But there is a big difference
in the functionalities of both. The syntax of bash until
loop can be defined as:
• until [ expression ];  
• do  
• command1  
• command2  
• . . .  
• . . . .   
• commandN  
• done  
• Until Loop with Single Condition
• In this example, the until loop contains a single condition in
expression. It is the basic example of until loop which will
print series of numbers from 1 to 10:
• Example
• #!/bin/bash  
• #Bash Until Loop example with a single condition  
•   
• i=1  
• until [ $i -gt 10 ]  
• do  
• echo $i  
• ((i++))  
• done  

You might also like