Shell scripting uses various types of statements to control the flow of execution, define logic, and interact with the system. These statements fall into different categories based on their function and purpose. Below is a detailed explanation of the different types of statements in shell scripting:
1. Comment Statements
- Used to provide explanations or annotations in the script.
- Begin with the # symbol.
Syntax:
# This is a comment
Example:
#!/bin/bash
# This script displays a welcome message
echo “Welcome to Shell Scripting!”
2. Variable Declaration Statements
- Used to define variables that store data values.
Syntax:
variable_name=value
Example:
#!/bin/bash
name=”John”
echo “Hello, $name!”
3. Input/Output Statements
- Handle user input and display output.
Input:
- Syntax:
read variable_name
- Example:
echo “Enter your name:”
read name
echo “Welcome, $name!”
Output:
- Syntax:
echo “text”
- Example:
echo “Hello, World!”
4. Decision-Making Statements
Control the flow of the script based on conditions.
a. if Statement
- Executes commands if a condition is true.
Syntax:
if [ condition ]; then
# commands
fi
Example:
#!/bin/bash
if [ -f “file.txt” ]; then
echo “file.txt exists”
fi
b. if-else Statement
- Provides an alternate path if the condition is false.
Syntax:
if [ condition ]; then
# commands
else
# commands
fi
Example:
#!/bin/bash
if [ -d “/home/user” ]; then
echo “Directory exists”
else
echo “Directory does not exist”
fi
c. elif Statement
- Allows multiple conditions to be checked sequentially.
Syntax:
if [ condition1 ]; then
# commands
elif [ condition2 ]; then
# commands
else
# commands
fi
Example:
#!/bin/bash
echo “Enter a number:”
read num
if [ $num -gt 10 ]; then
echo “Number is greater than 10”
elif [ $num -eq 10 ]; then
echo “Number is equal to 10”
else
echo “Number is less than 10”
fi
5. Looping Statements
Execute a block of code repeatedly.
a. for Loop
- Iterates over a list of items.
Syntax:
for variable in list; do
# commands
done
Example:
#!/bin/bash
for i in 1 2 3 4 5; do
echo “Number: $i”
done
b. while Loop
- Repeats as long as a condition is true.
Syntax:
while [ condition ]; do
# commands
done
Example:
#!/bin/bash
counter=1
while [ $counter -le 5 ]; do
echo “Counter: $counter”
((counter++))
done
c. until Loop
- Repeats until a condition becomes true.
Syntax:
until [ condition ]; do
# commands
done
Example:
#!/bin/bash
counter=1
until [ $counter -gt 5 ]; do
echo “Counter: $counter”
((counter++))
done
d. break and continue
- break: Exits the loop prematurely.
- continue: Skips the current iteration.
Example:
#!/bin/bash
for i in {1..5}; do
if [ $i -eq 3 ]; then
break
fi
echo “Number: $i”
done
6. Case Statements
- Used for multi-way branching.
Syntax:
case variable in
pattern1)
# commands ;;
pattern2)
# commands ;;
*)
# default commands ;;
esac
Example:
#!/bin/bash
echo “Enter a choice (1-3):”
read choice
case $choice in
1) echo “You chose option 1” ;;
2) echo “You chose option 2” ;;
3) echo “You chose option 3” ;;
*) echo “Invalid choice” ;;
esac
7. Functions
- Group a set of commands under a name.
Syntax:
function_name() {
# commands
}
Example:
#!/bin/bash
greet() {
echo “Hello, $1!”
}
greet John
8. Exit Statement
- Terminates the script or function.
Syntax:
exit status_code
Example:
#!/bin/bash
echo “Exiting script”
exit 0
9. Command Substitution
- Assigns the output of a command to a variable.
Syntax:
variable=$(command)
Example:
#!/bin/bash
date_today=$(date)
echo “Today’s date is: $date_today”
10. Arithmetic Statements
- Perform arithmetic operations.
Syntax:
result=$((expression))
Example:
#!/bin/bash
num1=10
num2=20
sum=$((num1 + num2))
echo “Sum: $sum”
Conclusion
Shell scripting provides a rich set of statements for implementing logic, iterating through data, and interacting with the system. Mastering these statements helps create efficient and effective scripts for automation and system management.