Table of contents
1.
Introduction
2.
Decision-Making in Shell Scripting
3.
The if-else Statements
3.1.
Syntax
3.2.
Implementation of the if-else Statements
3.3.
Example 1 (if-else-fi)
3.4.
Example 2 (if-elif-else-fi)
3.5.
Example 3 (Nested if-else-fi)
4.
The case-esac  Statement
4.1.
Syntax
4.2.
Implementation of the case-esac Statement
5.
Frequently Asked Questions
5.1.
What is the shell?
5.2.
Is shell easy to learn?
5.3.
Is Linux based on C or C++?
5.4.
Differentiate between Bash and shell?
5.5.
Do programmers use Linux?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Linux - Decision Making

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you used Linux as an operating system? Have you done some programming in the shell in Linux?

This article focuses on one of the critical shell programming concepts, Decision-Making. We will mainly concentrate on different statements you can use for decision-making in Shell. We will also learn the implementation using examples. Let’s see all these things in more detail.

Decision-Making in Shell Scripting

When you are programming or writing code, there are many essential concepts that help you on the way. One of these concepts is Decision-Making. This means "What to happen and when to happen."

You can use two types of decision-making statements.  

  1. The if-else Statement, and
     
  2. The case-esac Statement.
     

Let's check out these statements in detail.

The if-else Statements

The "if-else" Statement is conditional. You can use it to execute codes based on conditions. If the condition is true, it will run the code in the "if" block. If the condition is "false," it will execute the code in the "else" block.

You can even add more than two conditions by using the following:

  • if-elif-else-fi, or
     
  • nested if-else
     

Syntax

Now, let's check out the different syntaxes of the "if-else" Statement:

if-fi:

if [ condition ]; then
    statements/code to execute
fi

 

if-else-fi:

if [ condition ]
then
   statement 1/ code 1
else
   statement 2/ code 2
fi

 

if-elif-else-fi:

if [ condition 1 ]
then
   statement 1/ code 1
elif [ condition 2 ]
then
   statement 2/ code 2
else
   statement 3/ code 3
fi

 

Nested if-else:

if [ condition ]
then
   statement 1/ code 1
   if [ condition ]
   then
     statement/ code
   else
     statement/ code
   fi    
else
   statement 2/ code 2
fi

Implementation of the if-else Statements

Now, let’s try to understand the implementation of the if-else Statement using examples.

Example 1 (if-else-fi)

echo "Enter your age: "
read age

if [ "$age" -ge 17 ]; then
     echo "Congrats! You are an Adult..."
else
echo "Naah! You are still a kid..."
fi


Output:

output

Example 2 (if-elif-else-fi)

echo "Enter your age: "
read age

if [ $age -ge 17 ]; then
     echo "Congrats! You are an Adult..."
elif [ $age -eq 17 ]; then
     echo "Only 1 year to go..."
else
echo "Naah! You are still a kid..."
fi


Output:

output

Example 3 (Nested if-else-fi)

echo "Enter the name of the subject: "
read sub

if [ $sub == 'OS' ]
then
echo "Enter Your Marks: "
read score
        if [ $score -ge 59 ]
        then
        echo "Woohoo! You have passed..."
        else
        echo "Damn! You failed, bro..."
        fi
else
echo "You entered the Wrong Subject!!!"
fi

 

Output:

output

The case-esac  Statement

The "case-esac" Statement is like the switch-case Statement in Programming.

As you have seen above, you can also use "if-else" statements in case of multiple conditions. You can call it the multiway branch. But this is not always the best approach, especially in the case where the conditions of all the branches are dependent on a single variable. 

That's where "case-esac" comes in. In Unix Shell, you can use the "case-esac" statement to handle multiple conditions. It works more efficiently than the multiple "if-else" Statement.

Syntax

Now, let's check out the syntax of the "case-esac" statement:

case $var in
   case 1) Statement 1;;
   case 2) Statement 2;;
   case n) Statement n;;
esac

Implementation of the case-esac Statement

Now, let's check out an example to see the implementation of the "case-esac" Statement.

author="Shadow"

case "$author" in
#This is case 1.
"CSHellDestroyer") echo "The real name is Chetanya Saxena." ;;

#This is case 2.
"Shadow") echo "The real name is Amit Singh." ;;

#This is case 3.
     "CutiePie") echo "The real name is Aditi." ;;
esac


Output

output

Frequently Asked Questions

What is the shell?

Shell is a command language interpreter. It executes commands read from input devices like keyboards or files.

Is shell easy to learn?

Shells allow users to communicate efficiently and directly with their operating systems. Shell scripting is not a single language, but because it uses some natural language commands, it's easy to learn, even without a programming background.

Is Linux based on C or C++?

Linux is written mainly in C programming language, with some parts in assembly-level language.

Differentiate between Bash and shell?

Bash stands for “Bourne Again SHell.” It is an improvement of the sh, which is the original Bourne shell. Shell scripting is in any shell, whereas Bash scripting is specifically for Bash. sh is a shell command-line interpreter of Unix/Unix-like operating systems.

Do programmers use Linux?

Many developers and programmers tend to choose Linux Operating systems over other Operating Systems. This is because it enables them to work more effectively and quickly. It will allow them to customize as per their needs and be innovative. A massive perk of using Linux is that it is free to use and open-source.

Conclusion

In this article, we have studied the decision-making in Shell Programming in detail.

We hope that this article has provided you with the help to enhance your knowledge regarding the Linux-Shell and if you would like to learn more, check out our articles on Linux Kernel and Linux Directories.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass