Shell Script Conditional Logic and Flow Control

Shell Script Conditional Logic and Flow Control

Introduction

Shell scripting is a powerful tool for automating tasks and managing system operations. At its core, shell scripts rely on conditional logic and flow control to make decisions, repeat actions, and handle different scenarios. Understanding these concepts is essential for writing effective and robust shell scripts.

Conditional Logic

Conditional logic allows scripts to execute code based on specific conditions. The primary construct is the if statement, which tests a condition and executes code if the condition is true.

if Statement

The if statement evaluates a condition and executes the following code if the condition is true.

if [ "$variable" -eq 5 ]; then  
    echo "Variable is 5"  
fi  

This script checks if $variable equals 5 and prints a message if the condition is met.

if-else Statement

The if-else construct provides a fallback option. If the condition is false, the code inside the else block is executed.

if [ "$variable" -eq 5 ]; then  
    echo "Variable is 5"  
else  
    echo "Variable is not 5"  
fi  

This script outputs different messages based on the value of $variable.

elif Statement

The elif (else if) statement allows for multiple conditional checks. It is useful when the first condition is true, but a subsequent condition may also be true.

if [ "$variable" -eq 5 ]; then  
    echo "Variable is 5"  
elif [ "$variable" -eq 3 ]; then  
    echo "Variable is 3"  
else  
    echo "Variable is unknown"  
fi  

This script checks for values 5, 3, or any other value.

case Statement

The case statement is used to match a variable against a list of patterns. It is particularly useful for handling different cases in a script.

gender="male"
case "$gender" in  
    "male") echo "You are male"; ;;  
    "female") echo "You are female"; ;;  
    *) echo "Gender unknown"; ;;  
esac  

This script handles three possible gender values and outputs the corresponding message.

Flow Control

Flow control mechanisms in shell scripting allow scripts to repeat actions, skip code, or execute code based on specific conditions. The main loop constructs are for, while, until, and repeat.

for Loop

The for loop iterates over a sequence of values. It is commonly used to loop through files or numbers.

for i in {1..5}; do  
    echo "$i"  
done  

This script prints numbers 1 through 5.

while Loop

The while loop executes a block of code as long as a condition is true.

while [ "$count" -lt 10 ]; do  
    echo "$count"  
    count=$((count + 1))  
done  

This script increments a counter until it reaches 10.

until Loop

The until loop executes a block of code until a condition is met. It is useful when you want to run a script until a certain condition is satisfied.

until [ "$count" -ge 10 ]; do  
    echo "$count"  
    count=$((count + 1))  
done  

This script runs until the counter reaches 10.

repeat Loop

The repeat loop executes a block of code a specified number of times. It is often used in conjunction with break to exit the loop.

repeat 3  
    echo "Looping"  
    break  

This script runs the loop three times.

Conclusion

Conditional logic and flow control are fundamental to writing effective shell scripts. By using if, if-else, elif, and case statements, scripts can make decisions and handle different scenarios. Loops like for, while, until, and repeat allow for repetitive tasks and dynamic behavior. Mastering these concepts enables developers to create scripts that are both functional and flexible.