How to use Conditional Statements on Bash Script
To Use Conditional Statements On Bash Script
Introduction:
The conditional statement is used in any programming language to do any decision-making tasks. This statement is also used in bash to perform automated tasks like another programming language, just the syntax is a little bit different in bash. Bash using two types of conditional statements.
Installation Steps:
Step1: Check the Oracle Linux Version by using the below command
[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="9.2"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="9.2"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Oracle Linux Server 9.2"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:9:2:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 9"
ORACLE_BUGZILLA_PRODUCT_VERSION=9.2
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=9.2
Step 2: Next create one script file for if Condition by using Vim command
[root@linuxhelp ~]# vim ifscript
#!/bin/bash
read -p " Enter number : " number
if [ $number -eq 100 ] ## if the condition is true then run the condition
then
echo "Value is equal to 100"
fi
Step 3: To execute the script first change the executable file permission by using the following command
[root@linuxhelp ~]# chmod +x ifscript
Step 4: To list the file by using the following commands
[root@linuxhelp ~]# ll
total 8
-rw-r--r--. 1 root root 0 Sep 13 11:40 100
-rw-------. 1 root root 1062 Sep 4 17:37 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Desktop
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Documents
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Downloads
-rwxr-xr-x. 1 root root 176 Sep 13 12:34 ifscript
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Music
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Pictures
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Public
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Templates
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Videos
Step 5: To execute the script by using the following command
[root@linuxhelp ~]# ./ifscript
Enter number : 100
Value is equal to 100
Step 6: Again, execute the script by using the following command, if the conditions is not true
[root@linuxhelp ~]# ./ifscript
Enter number : 44
[root@linuxhelp ~]#
Step 7: Next create a one script file for if else Condition by using Vim command
[root@linuxhelp ~]# vim ifelse
#!/bin/bash
echo -n "Enter a number: "
read VAR
if [[ $VAR -gt 10 ]]
then
echo "The variable is greater than 10."
else
echo "The variable is equal or less than 10."
fi
Step 8: To execute the script first change the executable file permission by using the following command
[root@linuxhelp ~]# chmod +x ifelse
Step 9: To list the file by using the following commands
[root@linuxhelp ~]# ll
total 12
-rw-r--r--. 1 root root 0 Sep 13 11:40 100
-rw-------. 1 root root 1062 Sep 4 17:37 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Desktop
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Documents
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Downloads
-rwxr-xr-x. 1 root root 175 Sep 13 15:09 ifelse
-rwxr-xr-x. 1 root root 176 Sep 13 12:34 ifscript
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Music
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Pictures
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Public
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Templates
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Videos
Step 10: To execute the script by using the following command. If the first condition is true, it will be the output
[root@linuxhelp ~]# ./ifelse
Enter a number: 40
The variable is greater than 10.
Step 11: Again, execute the script by using the following command. If condition is false, it will be the output
[root@linuxhelp ~]# ./ifelse
Enter a number: 2
The variable is equal or less than 10.
Step 12: Next create a one script file for if elif Condition by using Vim command
[root@linuxhelp ~]# vim ifelif
#!/bin/bash
echo -n "Enter a number: "
read VAR
if [[ $VAR -gt 10 ]]
then
echo "The variable is greater than 10."
elif [[ $VAR -eq 10 ]]
then
echo "The variable is equal to 10."
else
echo "The variable is less than 10."
fi
Step 13: To execute the script first change the executable file permission by using the following command
[root@linuxhelp ~]# chmod +x ifelif
Step 14: To list the file by using the following commands
[root@linuxhelp ~]# ll
total 24
-rw-r--r--. 1 root root 175 Sep 13 15:12 @
-rw-r--r--. 1 root root 0 Sep 13 11:40 100
-rw-------. 1 root root 1062 Sep 4 17:37 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Desktop
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Documents
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Downloads
-rwxr-xr-x. 1 root root 232 Sep 13 15:47 ifelif
-rwxr-xr-x. 1 root root 175 Sep 13 15:09 ifelse
-rwxr-xr-x. 1 root root 176 Sep 13 12:34 ifscript
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Music
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Pictures
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Public
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Templates
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Videos
Step 15: To execute the script by using the following command. If the first condition is true, it will be the output
[root@linuxhelp ~]# ./ifelif
Enter a number: 100
The variable is greater than 10.
Step 16: To execute the script by using the following command. If the Second condition is true, it will be the output
[root@linuxhelp ~]# ./ifelif
Enter a number: 10
The variable is equal to 10.
Step 17: To execute the script by using the following command. If the first and second condition is false, it will be the output
[root@linuxhelp ~]# ./ifelif
Enter a number: 5
The variable is less than 10.
Step 18: Next create a one script file for Nested if Condition by using Vim command
[root@linuxhelp ~]# vim nestedif
#!/bin/bash
echo -n "Enter the first number: "
read VAR1
echo -n "Enter the second number: "
read VAR2
echo -n "Enter the third number: "
read VAR3
if [[ $VAR1 -ge $VAR2 ]]
then
if [[ $VAR1 -ge $VAR3 ]]
then
echo "$VAR1 is the largest number."
else
echo "$VAR3 is the largest number."
fi
else
if [[ $VAR2 -ge $VAR3 ]]
then
echo "$VAR2 is the largest number."
else
echo "$VAR3 is the largest number."
fi
fi
Step 19: To execute the script first change the executable file permission by using the following command
[root@linuxhelp ~]# chmod +x nestedif
Step 20: To list the file by using the following commands
[root@linuxhelp ~]# ll
total 24
-rw-r--r--. 1 root root 175 Sep 13 15:12 @
-rw-r--r--. 1 root root 0 Sep 13 11:40 100
-rw-------. 1 root root 1062 Sep 4 17:37 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Desktop
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Documents
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Downloads
-rwxr-xr-x. 1 root root 232 Sep 13 15:47 ifelif
-rwxr-xr-x. 1 root root 175 Sep 13 15:09 ifelse
-rwxr-xr-x. 1 root root 176 Sep 13 12:34 ifscript
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Music
-rwxr-xr-x. 1 root root 440 Sep 13 16:16 nestedif
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Pictures
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Public
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Templates
drwxr-xr-x. 2 root root 6 Sep 13 11:36 Videos
Step 21: To execute the script by using the following command
[root@linuxhelp ~]# ./nestedif
Enter the first number:10
Enter the second number:15
Enter the Thied number:20
20 is the largest number:
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to use Conditional Statements on Bash Script (if elif nestedif). Your feedback is much welcome.
Comments ( 0 )
No comments available