• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to use Conditional Statements in shell scripting on Oracle Linux 8.5

  • 00:42 cat /etc/os-release
  • 00:55 vim if.sh
  • 02:36 chmod 777 if.sh
  • 02:42 sh if.sh
  • 03:21 vim else.sh
  • 05:14 chmod 777 else.sh
  • 05:22 sh else.sh
  • 06:10 vim elif.sh
  • 07:03 chmod 777 elif.sh
  • 07:10 sh elif.sh
{{postValue.id}}

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.

Tags:
ethan
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

Which shell is used in Linux?

A

Bash. Bash, or the Bourne-Again Shell, is by far the most widely used choice and it comes installed as the default shell in the most popular Linux distributions.

Q

Is terminal A shell?

A

The terminal is a program that opens a graphical window and lets you interact with the shell.

Q

What are shell and kernel?

A

A shell is basically an interface present between the kernel and the user. A kernel is the very core of a typical OS

Q

What is the definition of a script file?

A

A script file is an m-file that contains a sequence of instructions but is not a function.

Q

How do I run a shell script?

A

Steps to write and execute a script
1. Open the terminal. 2. Go to the directory where you want to create your script.3.Create a file with .sh extension.4.Write the script in the file using an editor.Make the script executable with command chmod +x .5.Run the script using ./.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help David Lopez Guillen ?
Ayuda urgente instale SSL para servidor Opensuse y ahora no funciona tengo servicio web

hola segui este tutorial para tener un certificado ssl y ahora no se ve mi app en la red, espero alguien pueda ayudarme, tengo M9oodle en3.5 en un servidor open suse y ahora no funciona por favor ayuda.

https://www.linuxhelp.com/how-to-create-ssl-certificate-in-opensuse

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.