Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A 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!
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
Logical Operators in Shell Scripting
In a program, decisions are made using logical operators, also known as theboolean 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
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
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: -
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 problems, interview experiences, and interview bundles for placement preparations.
We wish you Good Luck! Please upvote our blog and help other ninjas grow.