
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bash If Elif Else Statement - A Comprehensive Tutorial
If you've been using command line interface on your computer, then you must be familiar with Bash, a popular shell that is used to run commands and scripts. Bash is a powerful tool that can help you automate various tasks and make your life easier. One of most important constructs in Bash is if-elif-else statement, which allows you to make decisions based on conditions. In this comprehensive tutorial, we will explore Bash if-elif-else statement and show you how to use it effectively.
What is Bash if-elif-else Statement?
The Bash if-elif-else statement is a construct that allows you to execute a block of code based on a set of conditions. It works like this: If a certain condition is true, then execute code associated with that condition. If that condition is false, then check next condition, and so on. If none of conditions are true, then execute code associated with else block.
Syntax of Bash if-elif-else Statement
The syntax of Bash if-elif-else statement is straightforward. Here's what it looks like ?
if [ condition1 ]; then # Code to be executed if condition1 is true elif [ condition2 ]; then # Code to be executed if condition2 is true else # Code to be executed if all conditions are false fi
Let's break this down ?
The if keyword is followed by a set of square brackets that enclose first condition. square brackets are used to test condition.
The then keyword indicates that code to be executed if condition is true is about to follow.
The elif keyword is followed by another set of square brackets that enclose second condition. elif block is optional and can be used if you have more than one condition to test.
The else block is optional and is executed if all conditions are false.
The fi keyword marks end of if-elif-else statement.
Examples of Bash if-elif-else Statement
Let's take a look at some examples to see how Bash if-elif-else statement works in practice.
Example 1: Checking if a File Exists
Suppose you want to check if a file exists before you perform some operation on it. Here's how you can do it using Bash if-elif-else statement ?
if [ -e file.txt ]; then echo "The file exists" else echo "The file does not exist" fi
In this example, we use -e option to test if file exists. If file exists, then we print a message saying that file exists. If file does not exist, then we print a message saying that file does not exist.
Example 2: Checking if a Number is Positive, Negative or Zero
Suppose you want to check if a number is positive, negative or zero. Here's how you can do it using Bash if-elif-else statement ?
read -p "Enter a number: " num if [ $num -gt 0 ]; then echo "The number is positive" elif [ $num -lt 0 ]; then echo "The number is negative" else echo "The number is zero" fi
In this example, we use -gt option to test if number is greater than zero. If number is greater than zero, then we print a message saying that number is positive. If number is less than zero, then we print a message saying that number is negative. If number is zero, then we print a message saying that the number is zero.
Example 3: Checking if a String is Empty or Not
Suppose you want to check if a string is empty or not. Here's how you can do it using Bash if-elif-else statement ?
read -p "Enter a string: " str if [ -z "$str" ]; then echo "The string is empty" else echo "The string is not empty" fi
In this example, we use -z option to test if string is empty. If string is empty, then we print a message saying that string is empty. If string is not empty, then we print a message saying that string is not empty.
Example 4: Checking if a User Exists
Suppose you want to check if a user exists on your system. Here's how you can do it using Bash if-elif-else statement ?
read -p "Enter username: " username if id "$username" >/dev/null 2>&1; then echo "The user exists" else echo "The user does not exist" fi
In this example, we use id command to check if user exists. If user exists, then we print a message saying that user exists. If user does not exist, then we print a message saying that user does not exist.
Advanced Usage of Bash if-elif-else Statement
The Bash if-elif-else statement is not limited to just checking simple conditions. You can use it to perform more advanced operations, such as pattern matching, file permissions checking, and more. Here are some examples of more advanced usage of Bash if-elif-else statement ?
Example 5: Checking if a file has Execute Permission
Suppose you want to check if a file has execute permission before you execute it. Here's how you can do it using Bash if-elif-else statement ?
if [ -x file.sh ]; then ./file.sh else echo "The file does not have execute permission" fi
In this example, we use -x option to test if file has execute permission. If file has execute permission, then we execute it. If file does not have execute permission, then we print a message saying that file does not have execute permission.
Example 6: Checking if a String Matches a Pattern
Suppose you want to check if a string matches a certain pattern. Here's how you can do it using Bash if-elif-else statement ?
read -p "Enter a string: " str if [[ "$str" =~ ^[A-Za-z]+$ ]]; then echo "The string contains only letters" else echo "The string does not contain only letters" fi
In this example, we use regular expressions to test if string contains only letters. If string contains only letters, then we print a message saying that string contains only letters. If string does not contain only letters, then we print a message saying that string does not contain only letters.
Example 7: Checking if a File is Newer Than Another File
Suppose you want to check if a file is newer than another file. Here's how you can do it using Bash if-elif-else statement ?
if [ file1 -nt file2 ]; then echo "file1 is newer than file2" else echo "file2 is newer than file1" fi
In this example, we use -nt option to test if file1 is newer than file2. If file1 is newer than file2, then we print a message saying that file1 is newer than file2. If file2 is newer than file1, then we print a message saying that file2 is newer than file1.
Tips for Using Bash if-elif-else Statement
Here are some tips to help you use Bash if-elif-else statement effectively ?
Always enclose variables in double quotes to prevent unexpected behavior when dealing with spaces and special characters.
Use descriptive variable names to make your code more readable.
Use indentation to make your code more readable.
Always test your code thoroughly before using it in production.
Conclusion
The Bash if-elif-else statement is a powerful construct that allows you to make decisions based on conditions. It's an essential tool for anyone who works with command line interface. In this tutorial, we've covered syntax and usage of Bash if-elif-else statement, as well as provided some examples to help you get started. With this knowledge, you'll be able to write Bash scripts that are more efficient and effective.