How to use Conditional Statements in Shell Scripting Debian 11.3
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.
Comments ( 0 )
No comments available