08. Bash if, else, and fi Statements
Overview¶
Bash if, else, and fi statements are used to make decisions in a shell script. They allow a script to execute different commands depending on whether a condition is true or false.
Basic Syntax¶
The if statement checks the condition.
The then keyword marks the commands that should execute when the condition is true.
The else block executes when the condition is false.
The fi keyword ends the if statement.
A space is required after [ and before ].
Incorrect:
Checking if a Directory Exists¶
The -d operator can be used to check whether a directory exists.
#!/bin/bash
directory="./BashScripting"
if [ -d $directory ]; then
echo "Directory exists"
else
echo "Directory does not exist"
fi
Explanation¶
Stores the directory path in the directory variable.
Checks whether the specified path is an existing directory.
The -d test returns true when the directory exists.
Runs when the directory exists.
Runs when the directory does not exist.
Marks the end of the if statement.
Example Output¶
When the directory does not exist:
Create the directory:
Run the script again:
Nested if/else¶
A nested if means placing one if statement inside another if statement.
Nested conditions can be used when a script needs to perform multiple levels of decision-making.
Example¶
#!/bin/bash
choice=4
echo "1. Bash"
echo "2. Scripting"
echo "3. Tutorial"
echo -n "Please choose a word [1,2 or 3]? "
while [ $choice -eq 4 ]; do
read choice
if [ $choice -eq 1 ]; then
echo "You have chosen word: Bash"
else
if [ $choice -eq 2 ]; then
echo "You have chosen word: Scripting"
else
if [ $choice -eq 3 ]; then
echo "You have chosen word: Tutorial"
else
echo "Please make a choice between 1-3 !"
echo "1. Bash"
echo "2. Scripting"
echo "3. Tutorial"
echo -n "Please choose a word [1,2 or 3]? "
choice=4
fi
fi
fi
done
Explanation¶
The variable is initially set to 4:
The value 4 is used to keep the while loop running until the user enters a valid choice.
The menu is displayed:
The script then asks the user for input:
The first condition checks whether the user selected 1:
If the choice is not 1, another if statement checks for 2:
If the choice is not 2, another condition checks for 3:
If none of the valid choices were entered, the script displays an error message and sets:
This causes the while loop to continue and display the menu again.
while Loop with Nested if¶
The loop uses:
This means the loop continues while choice is equal to 4.
When the user enters a valid value such as 1, 2, or 3, the value changes and the loop condition becomes false.
For example:
Please choose a word [1,2 or 3]? 5
Please make a choice between 1-3 !
1. Bash
2. Scripting
3. Tutorial
Please choose a word [1,2 or 3]? 2
You have chosen word: Scripting
The first input, 5, is invalid, so the script displays the menu again.
The second input, 2, is valid, so the script prints:
Common Bash Test Operators¶
Some commonly used test operators are:
| Operator | Purpose |
|---|---|
-d |
Check if a directory exists |
-f |
Check if a file exists |
-e |
Check if a path exists |
-r |
Check if a file is readable |
-w |
Check if a file is writable |
-x |
Check if a file is executable |
-eq |
Equal |
-ne |
Not equal |
-gt |
Greater than |
-lt |
Less than |
-ge |
Greater than or equal |
-le |
Less than or equal |
Key Points¶
-
ifis used to test a condition. -
thenstarts the commands executed when the condition is true. -
elsehandles the false condition. -
fiends theifstatement. -
Spaces are required inside
[ ]. -
-dchecks whether a directory exists. -
Nested
ifstatements allow multiple conditions to be checked. -
A
whileloop can be combined withif/elseto repeatedly process user input until a valid condition is met.