Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Easy

Shell scripting - If Statement

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

A shell is a command-line interpreter. The shell script is a computer program that runs on the Linux shell.  In this article, we will discuss how to use conditional statements in shell scripting. 

Condtional statements in Python

if statement

It is a conditional statement that will be executed when the condition specified is true. Mind the spaces in the syntax, they are important for execution.

Syntax

if [ expression ]
Statements
fi

Example

#!/bin/sh
a=10 


if [ $a == 10 ]
then 
       echo “a is equal to 10” 
fi
if [ $a != 10 ]
   then 
       echo “a is not equal to 10”
fi

Output

a is equal to 10
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

if - else statement

In this conditional statement, when the condition is true the if block is executed and when the condition is false the else block is executed. 

Syntax

if [ expression ]
then 
      statement 1
else 
      statement 2
fi

Example

#!/bin/sh
a=20
b=20
  
if [ $a == $b ]
then
    # If they are equal then print this
    echo "a is equal to b"
else
    #else print this
    echo "a is not equal to b"
fi

Output

a is equal to b

if-elif-else statements

In this conditional statement, when the condition is true the if block is executed and when the if condition is false, the elif condition is checked and finally when the elif condition is also false, the else block is executed.

Syntax

if [ expression1 ]
then
   statement1
elif [ expression2 ]
then
   statement2 
else
   statement3
fi

Example

#!/bin/sh
a=15
b=20
if [ $a == $b ]
then
   echo "a is equal to b"
elif [ $a -gt $b ]
then
   echo "a is greater than b"
elif [ $a -lt $b ]
then
   echo "a is less than b"
else
   echo "None of the condition met"
fi

Output

a is less than b

Nested if-else statement

Inside the normal if-else statement more if-else statements are present. 

Syntax:

if [ expression1 ]
then
   statement1
   statement2
   .
else
   if [ expression2 ]
   then
      statement3
      .
   fi
fi

Example:

#!/bin/sh

a = 10
b = 20
c = 5

if [$a -lt  $b]
then
if [$a -lt $c]
  then
  echo “a is less than b and c”
      else 
  echo “a is less than b and greater than c”
fi
else if [$a -gt $b]
then 
   echo “a is greater than b” 
else 
    echo “a and b are equal.”
fi

Output

a is less than b and greater than c.

Switch statement 

In the switch statement, the value of a variable is matched against predefined cases. If the specified value matches with any of the cases then it will execute the block of that particular case. When a match is found all of the associated statements until the double semicolon (;;) is executed. A case will be terminated when the last command is executed.

If there is no match, the exit status of the case is zero.

Syntax

case  in
   Pattern 1) Statement 1;;
   Pattern n) Statement n;;
esac

Example

#!/bin/sh
company="Google"
  
#Pass the variable in string
case "$company" in
    #case 1
    "Google") echo "CTC: 33 Lac" ;;
      
    #case 2
    "Microsoft") echo "CTC: 52 Lac" ;;
      
    #case 3
    "Amazon") echo "CTC: 45 Lac" ;;
esac

Frequently Asked Questions

What is a shell script file?

A file containing one or more commands is known as a shell script. Shell scripts offer a simple way to execute hard tasks, lengthy or complex sequences of commands, and repetitive operations. The system runs the command sequence in a shell script file when its name is entered.

Why is shell scripting used?

Admins use them to get regular backups. Compared to other languages like C or C++, they are simpler to write and debug. The shell script can be copied to other UNIX and related operating systems and run there. Shell scripts are also employed for routine system monitoring.

Is shell scripting easy?

Shell script syntax is similar to other languages. It would be quite simple to start with it if you have prior knowledge of any programming language, such as Python, C/C++, etc.

Conclusion

Congratulations on finishing this article. In this article, we discussed conditional statements in shell scripting. We saw examples of if, if-else, if-elif-else and switch statements with their syntaxes.

Do not stop learning! We recommend you to read some of our articles - 

🔥 Introduction to Linux

🔥 Linux Kernel

🔥 Linux Directories

🔥 Linux Commands list

Nmap commands

Head to the Guided Path on the Coding Ninjas Studio and upskill in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more courses.

If you want to Practice top Coding Problems, attempt mock tests, or read interview experiences, head to the Coding Ninjas Studio, our practice platform.

We wish you Good Luck!🎈 Please upvote our blogs if you find them useful 🏆 to help other ninjas grow.

Topics covered
1.
Introduction
2.
if statement
2.1.
Syntax
2.2.
Example
2.3.
Output
3.
if - else statement
3.1.
Syntax
3.2.
Example
3.3.
Output
4.
if-elif-else statements
4.1.
Syntax
4.2.
Example
4.3.
Output
5.
Nested if-else statement
5.1.
Syntax:
5.2.
Example:
5.3.
Output
6.
Switch statement 
6.1.
Syntax
6.2.
Example
7.
Frequently Asked Questions
7.1.
What is a shell script file?
7.2.
Why is shell scripting used?
7.3.
Is shell scripting easy?
8.
Conclusion