How to use Conditional Statements in shell scripting on Oracle Linux 8.5
To Use Conditional Statements In Shell Scripting On Oracle Linux 8.5
Introduction:
Shell scripts are text files that contain commands for UNIX-based operating systems. It is a list of commands in a computer program that is run by the UNIX shell which is a command line interpreter. The different operations executed by shell scripts are program execution, file manipulation, and text printing.
Procedure:
Step 1: Check the Oracle Linux 8.5 by using the below command
[root@linuxhelp ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.5"
ID="ol"
Step 2: Create Script file for if Statement by using the below command
[root@linuxhelp ~]# vim if.sh
#General syntax for if condition
#! /bin/bash
#General syntax
#if [ condition]; then
#command (s)
#fi
#! /bin/bash
echo "Enter a number"
read n
if [ $n -lt 100 ]; then
printf "$n is less than 100\n"
fi
Step 3: Grant Permission to the Script file by using the below command
[root@linuxhelp ~]# chmod 777 if.sh
Step 4: Run the Script file and Enter the number less than 100.Now the condition is satisfied so the output will be printed.
[root@linuxhelp ~]# sh if.sh
Enter a Number
85
85 is less than 100
Step 5: Run the Script file again and now enter the number greater than 100. Now the condition is not satisfied so the process exits.
[root@linuxhelp ~]# sh if.sh
Enter a number
110
Step 6: Create Script file for else Statement by using the below command
[root@linuxhelp ~]# vim else.sh
#General syntax for else condition
#! /bin/bash
#General syntax
#if [ condition]; then
#command (s)
#else
#command (s)
#fi
echo "Enter a number"
read n
if [ $n -lt 100 ]; then
printf "$n is less than 100\n"
else
printf "$n is greater than 100\n"
fi
Step 7: Grant Permission to the Script file by using the below command
[root@linuxhelp ~]# chmod 777 else.sh
Step 8: Run the Script file and Enter the number less than 100.Now the condition is satisfied so the output will be printed.
[root@linuxhelp ~]# sh else.sh
Enter a number
70
70 is less than 100
Step 9: Run the Script file again and now enter the number greater than 100. Now the condition is not satisfied with less 100 then so the other output will be printed.
[root@linuxhelp ~]# sh else.sh
Enter a number
150
150 is greater than 100
Step 10: Create Script file for if-elif-else Statement to check different conditions by using the below command
[root@linuxhelp ~]# vim elif.sh
#!/bin/bash
echo "Enter the mark"
read mark
if (( $mark >= 90 )); then
echo "Grade - A+"
elif (( $mark < 90 && $mark >= 80 )); then
echo "Grade - A"
elif (( $mark < 80 && $mark >= 70 )); then
echo "Grade - B+"
elif (( $mark < 70 && $mark >= 60 )); then
echo "Grade - C+"
else
echo "Grade - F"
fi
Step 11: Grant Permission to the Script file by using the below command
[root@linuxhelp ~]# chmod 777 elif.sh
Step 12: Run the Script file to check different conditions by using the below command
[root@linuxhelp ~]# sh elif.sh
Enter the mark
95
Grade - A+
[root@linuxhelp ~]# sh elif.sh
Enter the mark
50
Grade - F
[root@linuxhelp ~]# sh elif.sh
Enter the mark
75
Grade - B+
[root@linuxhelp ~]# sh elif.sh
Enter the mark
80
Grade - A
Conclusion:
We have reached the end of this article. In this guide, we have walked you through the steps required to Use Conditional Statements in Shell Scripting on Oracle Linux 8.5. Your feedback is much welcome.
Comments ( 0 )
No comments available