Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Operators in Shell Scripting
3.
Logical Operators in Shell Scripting
4.
Logical OR (||) in Shell Scripting
5.
Logical AND (&&) in Shell Scripting
6.
Multiple Logical OR & AND in Shell Scripting
7.
Frequently Asked Questions 
7.1.
What does the dollar sign ($) in shell scripts mean?
7.2.
Why is a shell script used?
7.3.
What is the tput command in shell scripts?
7.4.
What does the . (dot) at the start of the file, name mean?
8.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Shell Scripting - Logical 'AND' and 'OR' Operator

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

Introduction

shell script is a computer program designed to be run by a command-line interpreter Unix shell. Shell scripts can execute programs, manipulate files, and print text, among other things.

In this blog, we will discuss Logical ‘AND’ ‘OR’ Operator in Shell Scripting in deep detail. Let’s start going!

Shell Scripting  - Logical 'AND' & 'OR' Operator

Operators in Shell Scripting

An operator is a character that decides the course of action to be taken or factors to be taken in computer science. There are mainly five types of operators available in shell scripting:-

⚡ Arithmetic Operators

⚡ Relational Operators

⚡ Logical Operators

⚡ Bitwise Operators

⚡ File Tests Operators

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

Logical Operators in Shell Scripting

In a program, decisions are made using logical operators, also known as the boolean operators, which lead to executing various sets of instructions. This is also referred to as flow control.

When two or more conditions combine to produce a single result, that is the creation of a logical condition. Here, the result of one condition could also be the opposite of that of another condition.

Logical OR (||) in Shell Scripting

Logical OR (||) combines two or more conditions. Any one of the conditions that return true causes it to return true. The first condition is always verified, but the second condition is only verified if the first condition is false. It is denoted by -o in shell scripts.

The given below scripts shows the use of Logical OR (||):

#!/bin/bash
 
read -p "Try your Guess: " num

if [[ $num%2==0 || $num%5==0 ]]  # if num is divisible by 2 or 5, it is right guess  
then     
  echo "You guess correctly."
else
  echo "Better Luck Next Time."
fi

 

Input

10

 

Output

You guess correctly.

Logical AND (&&) in Shell Scripting

Logical AND (&&) is used when two or more conditions are present and it returns true when all the conditions are true. While the first condition is always verified, the second condition is only verified if the first one is true. -a denotes it in shell scripts.

The given below scripts show the use of Logical AND (&&):

!/bin/bash   

read -p n

if [ $((n % 2)) == 0 ] && [ $((n % 5)) == 0 ];
then
    echo "Number is even and also divisible by 5"
else
    echo "Number is odd and not divisible by 5"
fi

 

Input

10

 

Output

Number is even and also divisible by 5

Multiple Logical OR & AND in Shell Scripting

Multiple Logical OR & AND in Shell Scripting

We can use multiple Logical OR & AND operators in one single-line statement. The given below scripts show the use of multiple logical operators in one statement:

#!/bin/bash

# Check if the number is negative or positive
# Or if the number is positive, it lies within 50-60 or not
 
read -p "Please enter a number: " n
 
if ( [ $n -le 0 ]) || ([ $n -ge 50 ] && [ $n -le 60 ])
then
     echo "Number is negative or between 50 and 60"
else
     echo "Input number ($n) didn’t exist"
fi

 

Input

40

 

Output

Input number (40) didn’t exist

 

Read about Bitwise Operators in C here.

Frequently Asked Questions 

What does the dollar sign ($) in shell scripts mean?

The "$?" symbol is used in conjunction with an "if statement" when writing a shell script to determine whether or not the previous command was successfully executed.

Why is a shell script used?

System administrators use it to issue various commands to complete the task. All commands are combined in a text file (shell script) for daily routine tasks.

What is the tput command in shell scripts?

tput command allows us to modify the output's presentation. Regardless of the screen size, shell scripts can also center and underline text, among other things.

What does the . (dot) at the start of the file, name mean?

It is a hidden file if the file name starts with ".". In most file managers and shell programs, a file is hidden when its name begins with a dot. When you attempt to list the files in a shell, it typically does so except for hidden files. Despite this, the directory contains hidden files. If you want to see hidden files, you must use the "-a" flag when running the Is command.

Conclusion 

Congratulations on finishing the blog! We have discussed the Logical 'AND' & 'OR' Operator in Shell Scripting. We further discussed using multiple logical operators OR & AND in shell scripting.

We hope this blog has helped you enhance your knowledge of Logical Operators in Shell Scripting. Do not stop learning! We recommend you read some of our Shell Scripts articles: -

✴️ Introduction to Linux Shell & Scripting Shell

✴️ Shell Scripting Interview Questions

✴️ Crontab in Linux

✴️ gzip command in linux
 

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundles for placement preparations.

We wish you Good Luck! Please upvote our blog and help other ninjas grow.

Happy Learning!

Live masterclass