Bash Script | How to Use the Condition or with if Statement?

In Bash scripting for Linux, the “or” and “if” statements are used to control the flow of a script by allowing the user to specify conditions that must be met for certain commands to be executed. The “or” statement (||) allows the execution of a command if one of the multiple conditions is met, while the “if” statement allows the execution of a command if a single condition is met. In this article, we will discuss how to effectively use the “or” and “if” statements together in Bash scripting to create powerful and efficient scripts.

This guide will teach the audience about using the condition “or” with the “if” statement.

Using if Statement in Bash Script

We will first show you how to apply the if command to any bash script so you can understand it better. Afterward, we will show you how to incorporate it with the ‘or’ command in the next section.

A Bash script named “bashfile2.sh” is created. The code of it is shown in the snippet below: 

#/bin/bash
echo -n "Enter a number: "
read No
if [[ $No -gt 10 ]]
then
echo "Number is greater than 10. "
else
echo "The number is less than 10. "
fi

Explanation of a Code: 

We have started with a basic if command and are making a comparison here in which a user will provide any number as an input inside a ‘No variable. The “if” condition checks the entered number for being less or greater than 10.

If the condition is true, it will print ‘The number is greater than 10’; if not, ‘the number is less than 10’ using the echo command. Also, when you are using an if statement, you need to end its body by writing ‘fi’ otherwise, it will show you a syntax error.

To execute the above-created script, we used the following command:

$ bash bashfile2.sh

After adequately understanding the syntax and usage of the if condition, let’s move to the ‘or’ condition combined with the ‘if’ statement in the next section.

Using “OR” With “if” in Bash 

To use the ‘or’ condition in the bash program, you must write two straight vertical lines ( || ). It is used to compare at least two different conditions; if any of them are true, then their output will also be true. 

Example 1: To Check If a Number is between 10 and 20

The basic example of the “or” and “if” combined is mentioned below:

#/bin/bash
echo "Enter any number between 10 and 20"
read No
if [[ ( $No -lt 10 || $No -gt 20 ) ]]
then
echo "Number is not in range"
else
echo "You have entered $No"
fi

Explanation of a Code: 

In the code, the user will be asked to write a random number between 10 to 20. If a user writes any number that does not belong to this range, it will print ‘Number is not in range’; otherwise, it will display that number.

Example 2: To Check If a Number is Even or Divisible by 5

In another example, we’d check whether the entered number is even or can be divided by five, and for that, we’ve created a bash file, as seen below.

#!/bin/bash
echo -n "Enter a Number: "
read num
if [ $((num % 2)) == 0 ] || [ $((num % 5)) == 0 ];
then
    echo "You entered $num, which is either even or divisible by 5."
else 
    echo “You entered $num, which is not an even number or divisible by 5."
fi

Explanation of the code: 

Here we’ve asked the user to enter a number, and then the program will check if a number is even or divisible by 5 or not.

And now, we’ll execute the above-created script using this command.

$ bash newfile.sh

We have executed the script two times where we first entered the number 3, and in this case, both conditions are false. In the section execution, we have entered 15 divisible by 5, so this condition is true. 

By Logical OR (||), we mean that either one of the “if” statements are true, and the command(s) would be executed.

Conclusion

In Bash scripting for Linux, “or” and “if” statements are powerful tools that allow you to control the flow of a script by specifying conditions that must be met for certain commands to be executed.  In this tutorial, we have demonstrated how to effectively use these statements together in Bash scripting, creating efficient and powerful scripts.