Bash, also known as the Bourne Again Shell, is a widely used command language interpreter in Unix, Linux, and Mac OS environments. It provides powerful tools and syntax to automate tasks and execute commands. One essential aspect of Bash scripting is the usage of conditional statements, such as if, else if, and else, which allow you to control the flow of your script based on certain conditions. In this article, we will explore how to effectively utilize these conditional statements in Bash scripting and master their usage.
If you intend to buy a Windows VPS, we suggest you to check the various plans offered on our website and start the plan you need.
The if statement is the fundamental conditional statement in Bash scripting. It enables you to execute a block of code based on the evaluation of a specific condition. The basic syntax of an if statement is as follows:
if condition
then
# code block to execute if the condition is true
fi
Here, the condition can be any valid expression or command that returns a status of 0 (true) or non-zero (false). If the condition evaluates to true, the code block within the if statement is executing; otherwise, it is skipping.
Example:
In the following example, we use the “if” statement to check if the variable “number” is greater than 10. If the condition is true, it will execute the “echo” statement and output “Number is greater than 10“.
#!/bin/bash
number=15
if [ $number -gt 10 ]
then
echo "Number is greater than 10"
fi
The else statement comes into play when the condition of the if statement is false. Its syntax is straightforward:
if condition
then
# code block to execute if the condition is true
else
# code block to execute if the condition is false
fi
In this case, if the condition evaluates to true, the code block within the if statement is executing. However, if the condition is false, the code block within the else statement will be execute.
Example:
In the following example, the user is prompted to enter their age, and then the script checks if the age is greater than or equal to 18 using the “if” statement. If the condition is true, it prints “You are eligible to vote.” Otherwise, it prints “You are not eligible to vote yet.“
#!/bin/bash
# Prompt the user to enter their age
read -p "Enter your age: " age
# Check if the user is old enough to vote (age >= 18)
if [ $age -ge 18 ]; then
echo "You are eligible to vote."
else
echo "You are not eligible to vote yet."
fi
There are scenarios where you might want to check multiple conditions. The else if statement allows you to evaluate additional conditions after the initial if statement. Its syntax is as follows:
if condition1
then
# code block to execute if condition1 is true
elif condition2
then
# code block to execute if condition2 is true
else
# code block to execute if none of the conditions are true
fi
Here, condition1 is evaluating first.
1- If it is true, the code block within the corresponding if statement is executing.
2- If condition1 is false, condition2 is evaluating.
3- If condition2 is true, the code block within the elif statement is executing.
4- If none of the conditions are true, the code block within the else statement will be execute.
Example:
In the following example, the script takes a number from the user and checks if it is 0, positive, or negative using the “if”, “elif” (short for ‘else if’), and “else” statements. Depending on the value of the number, it will execute the corresponding code block.
#!/bin/bash
read -p "Enter a number: " number
if [ $number -eq 0 ]; then
echo "Number is zero."
elif [ $number -gt 0 ]; then
echo "Number is positive."
else
echo "Number is negative."
fi
It is also possible to nest if statements within each other to handle complex conditions. This allows you to have multiple levels of conditions. The inner if statements are typically surrounding by additional indentation to enhance readability.
Example:
In this example, we prompt the user to enter their age. The script then checks if the age is greater than or equal to 18 using the first if statement. If it is true, it prints “You are eligible to vote” and proceeds to the nested if statement.
The nested if statement checks if the age is greater than or equal to 21. If it is true, it prints “You are also eligible to drink alcohol.” Otherwise, it prints “You are not eligible to drink alcohol.“
If the initial condition of the first if statement is false, it skips to the else statement and prints “You are not eligible to vote.“
bash
#!/bin/bash
read -p "Enter your age: " age
if [[ $age -ge 18 ]]; then
echo "You are eligible to vote."
if [[ $age -ge 21 ]]; then
echo "You are also eligible to drink alcohol."
else
echo "You are not eligible to drink alcohol."
fi
else
echo "You are not eligible to vote."
fi
Note: The double square brackets “[[” are used for conditional expressions in Bash. The “-ge” operator is used to check if the value on the left is greater than or equal to the value on the right.
Mastering the usage of if, else if, and else statements in Bash scripting is crucial for creating robust and flexible scripts. These conditional statements provide the means to control the flow of your script based on various conditions, allowing you to handle different scenarios efficiently. By harnessing the power of these statements, you can automate tasks, make decisions, and handle errors effectively in your Bash scripts, ultimately becoming a more proficient Bash scripter.
How useful was this post?
Click on a star to rate it!
Average rating 5 / 5. Vote count: 1
No votes so far! Be the first to rate this post.
In this tutorial, we are going to teach you How to Install DirectAdmin on Dedicated Server. The web ...
After reading this article, you will be completely familiar with Create a FTP Account with SSH on De...
What is your opinion about this Blog?