Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views8 pages

Shell Scripting Chapter No7 PDF

Chapter 7 discusses control statements in shell scripting, including if, if-else, if-elif-else, nested if, and switch statements. Each control statement is explained with syntax and examples, illustrating how to execute code based on conditions. The chapter emphasizes the importance of proper syntax in shell scripting, as it is case-sensitive.

Uploaded by

bagpackeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Shell Scripting Chapter No7 PDF

Chapter 7 discusses control statements in shell scripting, including if, if-else, if-elif-else, nested if, and switch statements. Each control statement is explained with syntax and examples, illustrating how to execute code based on conditions. The chapter emphasizes the importance of proper syntax in shell scripting, as it is case-sensitive.

Uploaded by

bagpackeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter-7 Shell Script Control Statements

Control statements are also known as conditional statements.


In shell Scripting also there are different types of control statements are present like other
programming language.
Following are the different control statements
if statement
if-else statement
if..elif..else..fi statement (Else If ladder)
if..then..else..if..then..fi..fi..(Nested if)
switch statement

1) IF Control statement:
This Block will be execute if the provided if condition is true
Syntax:
if [ expression ]
then
statement
fi
1) Example:
#Initializing two variables
a=10
b=20
#Check whether they are equal
if [ $a == $b ]; then
echo "a is equal to b"
fi

2) Example:
#Initializing two variables
a=10
b=20

#Check whether they are not equal


if [ $a != $b ]
then
echo "a is not equal to b"
fi
2) if-else Control statement:
This Block will be execute when provided if condition in if block is not true, then system will jump
to else block and execute that block.
Syntax:
if [ expression ]
then
statement1
else
statement2
fi
Example:
#Initializing two variables
a=10
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
3)if..elif..else..fi statement (Else If ladder)
To use multiple conditions in one if-else block, then 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 condition is true then it processes else part.
Syntax:
if [ expression1 ]
then
statement1
statement2
.
.
elif [ expression2 ]
then
statement3
statement4
.
.
else
statement5
fi
4) if..then..else..if..then..fi..fi..(Nested if)
Nested if-else block can be used when, one condition is satisfies then it again checks another
condition. I
in the syntax, if expression1 is false then it processes else part, and again expression2 will be check.
Syntax:
if [ expression1 ]
then
statement1
statement2
.
else
if [ expression2 ]
then
statement3
.
fi
fi
5) switch statement:
case statement works as a switch statement if specified value match with the pattern then it will
execute a block of that particular pattern.
When a match is found all of the associated statements until the double semicolon (;;) is executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
Syntax:
case in
Pattern 1) Statement 1;;
Pattern 2) Statement 2;;
..........................
Pattern n) Statement n;;
esac
Example:
CARS="bmw"

#Pass the variable in string


case "$CARS" in
#case 1
"mercedes") echo "Headquarters - Affalterbach, Germany" ;;

#case 2
"audi") echo "Headquarters - Ingolstadt, Germany" ;;

#case 3
"bmw") echo "Headquarters - Chennai, Tamil Nadu, India" ;;
esac

Note: Shell scripting is a case-sensitive language, which means proper syntax has to be followed
while writing the scripts.
fExample 2:

#!/bin/bash

fruits='apple'

case "$fruits" in
"banana") echo "cost of banana is 250";;
"lemon") echo "cost of lemon is 150";;
"apple") echo "cost of apples is 500";;
esac

You might also like