Control structures are fundamental components in shell scripting that allow you to control the flow of your script based on conditions and loops. In this section, we will cover the following key concepts:
- Conditional Statements
ifstatementscasestatements
- Looping Constructs
forloopswhileloopsuntilloops
- Break and Continue
- Conditional Statements
if Statements
The if statement is used to execute a block of code only if a specified condition is true.
Syntax:
if [ condition ]; then
# code to execute if condition is true
elif [ another_condition ]; then
# code to execute if another_condition is true
else
# code to execute if none of the conditions are true
fiExample:
#!/bin/bash
echo "Enter a number:"
read number
if [ $number -gt 10 ]; then
echo "The number is greater than 10."
elif [ $number -eq 10 ]; then
echo "The number is equal to 10."
else
echo "The number is less than 10."
ficase Statements
The case statement is used to simplify complex if-elif-else chains. It matches a variable against a set of patterns and executes the corresponding block of code.
Syntax:
case $variable in
pattern1)
# code to execute if variable matches pattern1
;;
pattern2)
# code to execute if variable matches pattern2
;;
*)
# code to execute if variable doesn't match any pattern
;;
esacExample:
#!/bin/bash
echo "Enter a letter:"
read letter
case $letter in
[a-z])
echo "You entered a lowercase letter."
;;
[A-Z])
echo "You entered an uppercase letter."
;;
*)
echo "You entered a non-alphabetic character."
;;
esac
- Looping Constructs
for Loops
The for loop is used to iterate over a list of items and execute a block of code for each item.
Syntax:
Example:
while Loops
The while loop executes a block of code as long as a specified condition is true.
Syntax:
Example:
until Loops
The until loop is similar to the while loop, but it executes a block of code as long as a specified condition is false.
Syntax:
Example:
- Break and Continue
break
The break statement is used to exit from a loop prematurely.
Example:
continue
The continue statement is used to skip the current iteration of a loop and proceed to the next iteration.
Example:
Practical Exercises
Exercise 1: Basic if Statement
Write a script that asks the user for their age and prints whether they are a minor, an adult, or a senior.
Solution:
#!/bin/bash
echo "Enter your age:"
read age
if [ $age -lt 18 ]; then
echo "You are a minor."
elif [ $age -le 65 ]; then
echo "You are an adult."
else
echo "You are a senior."
fiExercise 2: for Loop with break
Write a script that prints numbers from 1 to 10 but stops if the number is 7.
Solution:
Exercise 3: while Loop with continue
Write a script that prints numbers from 1 to 10 but skips the number 5.
Solution:
#!/bin/bash
count=1
while [ $count -le 10 ]; do
if [ $count -eq 5 ]; then
count=$((count + 1))
continue
fi
echo "Number: $count"
count=$((count + 1))
doneConclusion
In this section, we covered the essential control structures in shell scripting, including conditional statements (if and case), looping constructs (for, while, and until), and the use of break and continue statements. These control structures are crucial for writing effective and efficient shell scripts. Practice the provided exercises to reinforce your understanding and prepare for more advanced scripting topics.
Linux Mastery: From Beginner to Advanced
Module 1: Introduction to Linux
Module 2: Basic Linux Commands
- Introduction to the Command Line
- Navigating the File System
- File and Directory Operations
- Viewing and Editing Files
- File Permissions and Ownership
Module 3: Advanced Command Line Skills
- Using Wildcards and Regular Expressions
- Piping and Redirection
- Process Management
- Scheduling Tasks with Cron
- Networking Commands
Module 4: Shell Scripting
- Introduction to Shell Scripting
- Variables and Data Types
- Control Structures
- Functions and Libraries
- Debugging and Error Handling
Module 5: System Administration
- User and Group Management
- Disk Management
- Package Management
- System Monitoring and Performance Tuning
- Backup and Restore
Module 6: Networking and Security
- Network Configuration
- Firewall and Security
- SSH and Remote Access
- Intrusion Detection Systems
- Securing Linux Systems
Module 7: Advanced Topics
- Virtualization with Linux
- Linux Containers and Docker
- Automating with Ansible
- Linux Kernel Tuning
- High Availability and Load Balancing
