• 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 Debian 11.3

  • 00:24 lsb_release -a
  • 00:36 sudo -i
  • 01:03 touch if.sh
  • 01:12 ls -la
  • 01:24 chmod 755 if.sh
  • 01:37 ls -la
  • 01:50 nano if.sh
  • 03:35 sh if.sh
  • 04:04 touch else.sh
  • 04:19 chmod 755 else.sh
  • 04:39 nano else.sh
  • 05:19 sh else.sh
  • 05:36 touch elif.sh
  • 05:47 chmod 755 elif.sh
  • 05:59 nano elif.sh
  • 06:30 sh elif.sh
{{postValue.id}}

To Use Conditional Statements in Shell Scripting Debian 11.3.

Introduction

The if...elif...else statement is the one level advance form of control statement that allows Shell to make correct decision out of several conditions.

Step 1: Check whether the Debian OS version is installed by using the below command

linuxhelp@linuxhelp: ~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Debian
Description:	Debian GNU/Linux 11 (bullseye)
Release:	11
Codename:	bullseye

Step 2: Log in as root user by using the below command.

linuxhelp@linuxhelp: ~$ sudo -i
[sudo] password for linuxhelp: 

Step 3: Create file named as if.sh by using the below command.

root@linuxhelp: ~# touch if.sh

Step 4: Long list the files by using the below command.

root@linuxhelp: ~# ls -la
total 32
drwx------  4 root root 4096 Jul  9 08:24 .
drwxr-xr-x 18 root root 4096 Mar 26 21:39 ..

-rw-------  1 root root 1515 Jul  9 08:22 .bash_history
-rw-r--r--  1 root root  571 Apr 11  2021 .bashrc
drwxr-xr-x  4 root root 4096 May 25 02:10 .cache
-rw-r--r--  1 root root    0 Jul  9 08:24 if.sh
drwxr-xr-x  3 root root 4096 May 25 02:42 .local
-rw-------  1 root root   48 May 25 02:40 .mysql_history

Step 5: Assign Permission to if.sh file.

root@linuxhelp: ~# chmod 755 if.sh 

step 6: Long list the files by using the below command.

root@linuxhelp:~# ls -la
total 32
drwx------  4 root root 4096 Jul  9 08:24 .
drwxr-xr-x 18 root root 4096 Mar 26 21:39 ..
-rw-------  1 root root 1515 Jul  9 08:22 .bash_history
-rw-r--r--  1 root root  571 Apr 11  2021 .bashrc
drwxr-xr-x  4 root root 4096 May 25 02:10 .cache
-rwxr-xr-x  1 root root    0 Jul  9 08:24 if.sh
drwxr-xr-x  3 root root 4096 May 25 02:42 .local
-rw-------  1 root root   48 May 25 02:40 .mysql_history

Step 7: Now edit the if.sh file.

root@linuxhelp:~# nano 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 8: Now Run the if.sh file by using the below command.

root@linuxhelp: ~# sh if.sh
Enter the number
80
80 is less than 100

Step 9: Next create file name as else.sh.

root@linuxhelp: ~# touch else.sh

Step 10: Now Assign Permission to else.sh file.

root@linuxhelp: ~# chmod 755 else.sh 

step 11: Next, Edit the else.sh file by using the below command

root@linuxhelp: ~# nano 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 12: After editing the file Run the else.sh.

root@linuxhelp: ~# sh else.sh
Enter the number
150
80 is greater than 100

Step 13: Next Create elif.sh file by using the below command.

root@linuxhelp: ~# touch elif.sh

Step 14: Assign Permission to elif.sh file.

root@linuxhelp: ~# chmod 755 elif.sh 

Step 15: Edit the elif.sh file by using the below command.

root@linuxhelp: ~# nano elif.sh 
#General syntax for elif condition
#! /bin/bash
#General syntax
#if [ condition]; then
#command (s)
#elif [ condition]; then
#command (s)
#else
#command (s)
#fi

echo “Enter a number”
read n
if [ $n -lt 100 ] ; then
Printf “$n is equal to 100\n
elif [ $n -lt 100 ] ; then
Printf “$n is greater than 100\n”
else
Printf “$n is lesser than 100\n”
fi

Step 16: Finally Run the elif.sh file by using the below command.

root@linuxhelp:~# sh elif.sh
Enter the number
100
100 is equal to 100

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 Debian 11.3. Your feedback is much welcome.

Tags:
muhammad
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

Is shell scripting and bash scripting the same?

A

Bash is the acronym for "Bourne Again Shell", which is also considered to be a subset of shell scripting.
Shell scripting is scripting in any shell and eases the interaction between the user and operating system, while bash scripting can be done only for the bash.

Q

Types of Conditional statements?

A

There are various types of conditional statements in Bash:
if statement
if-else statement
if..elif..else statement
Nested

Q

What is if, else, elif?

A

The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False, it checks the condition of the next elif block and so on. If all the conditions are False, the body of else is executed.

Q

How to create a script file?

A

Touch .sh

Q

How to execute the script file?

A

Sh.

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.