Linux System
Administration
CHAPTER-3 Control Statements and
Functions
Content
• Package Management with APT and YUM
• Kernal Updates and System Reboots
• Log file Analysis and Troubleshooting
• Monitoring System Performance
Control Statements and Functions
• Introduction
• Normally, shell executes commands sequentially (one after another).
• Sometimes we need to:
• Run only one of multiple commands (decision making).
• Repeat a command multiple times (iteration).
• Control Structures in shell:
• Selection (Decision making):
• if/then/elif/else/fi
• case
• Iteration (Looping):
• for
• while
if-then Statement
• Definition
o The if-then statement executes commands based on a condition.
• Purpose
o To control program flow by running commands only if a condition is true.
• Syntax • Explanation:
if command OR o if → starts the condition
then if command; then o then → tells the shell what to execute if
commands the condition is true
commands fi o fi → marks the end of the if block
fi (just "if" spelled backward)
• Example:
#!/bin/bash
# using if-then
if whoami; then
var1=$(whoami)
echo "Welcome $var1"
fi
if date; then
echo "Yipee its working"
fi
if-then-else Statement
• Definition
o The if-then-else statement provides two choices:
o Execute one block if condition is true.
o Execute another block if condition is false.
• Syntax
if command
then
commands
else
commands
fi
• Example:
#!/bin/bash
if whoam; then
echo "This will never print"
else
echo "You have used wrong command"
fi
if-elif-else-fi (else-if ladder)
• Definition
o The if-elif-else-fi statement allows checking multiple conditions sequentially.
• Purpose
o To select one block of code among many based on conditions.
• Syntax
if [ condition1 ] elif [ condition3 ]
then then
statement1 statement3
elif [ condition2 ] else
then statementN
fi
statement2
• Example:
#!/bin/sh
a=10
b=20
if [ $a -eq $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
case Statement
• Definition
o The case statement matches a variable against patterns and executes corresponding
commands.
• Purpose
o Useful for multi-way branching (similar to switch-case in C/Java).
• Syntax
case variable in
pattern1) commands;;
pattern2) commands;;
pattern3) commands;;
*) default commands;;
esac
• Example:
#!/bin/bash
echo "Enter your favorite fruit name"
read fruit
case "$fruit" in
"apple") echo "An apple a day keeps doctor away!!" ;;
"banana") echo "Yellow yellow banana!! yummy yummy banana!!" ;;
"mango") echo "I am king of fruits!!" ;;
*) echo "invalid data" ;;
esac
for Loop
• Definition
o The for loop executes a block of code repeatedly for each value in a list.
• Purpose
o To iterate over a list of values or a sequence.
• Syntax
for var_name in list Example:
#!/bin/bash
do
i=1
command1
for day in Mon Tue Wed Thu Fri
command2 do
done echo "Weekday $((i++)): $day"
done
While Command
• Definition
o The while statement is used to execute a list of commands repeatedly.
o It keeps executing as long as the test command returns a zero exit status (true).
o When the test command returns a non-zero exit status (false), the loop stops.
• Syntax
while test_command
do
other_commands
done
Example 1: Using a Counter
i=1 Output:
while [ $i -le 10 ] i= 1
do i= 2
echo "i= $i" ...
i=$[ $i + 1 ] i= 10
done
Example 2: Reverse a 3-digit Number
echo "Enter a 3 digit number"
do
read num
digit=$(( $num % 10 ))
rev=0
rev=$(( $rev * 10 + $digit ))
while [ $num -gt 0 ]
num=$(( $num / 10 ))
done
echo "Reversed number: $rev"
Summary Table
Statement Type Purpose Example Use
if-then Runs code if condition is true Check if user is logged in
if-then-else Chooses between two actions Login success/failure
if-elif-else Multiple conditions checking Compare two numbers
case Multi-way branching Menu selection
Repeats commands for list
for loop Loop through weekdays
items
Repeats commands as long as
while loop Print numbers 1 to 10
condition is true
• Unlike many programming languages (like C, Java, Python) that use { } or indentation to close
blocks, Bash uses keywords.
o if ... fi, case ... esac, for ... done, while ... done
Linux Shell Functions
• Definition
o A function is a reusable block of code.
o It helps in code reusability and makes shell programs shorter and easier to maintain.
o Functions can also accept parameters.
• Syntax
o function_name()
o{
o list of commands
o}
Example 1: Function Without Parameters
#!/bin/sh
# Define function
hello()
{
echo "Welcome to Linux"
}
# Invoke function
Hello
Output: Welcome to Linux
Example 2: Function with Parameters
#!/bin/sh
# Define function
Hello()
{
echo "Welcome to Linux $1 $2"
}
# Invoke function with parameters
Hello Amar Salunkhe
Output: Welcome to Linux Amar Salunkhe