Shell Scripting: Logical Operators

Samuel Kadima
5 min readApr 28, 2023

--

In shell scripting, logical operators are used to test multiple conditions and determine the outcome of the script based on those conditions. Logical operators can be used to create complex conditional statements that allow for more dynamic and flexible scripts. These operators include AND, and OR.

By understanding how to use logical operators in shell scripting, developers can create more robust and efficient scripts that can handle a wide range of scenarios.

This article will explore the basics of logical operators and provide examples of how they can be used in real-world scripts.

Steps

Navigate to your working directory or create a new one and navigate to it using the following command

mkdir shell_scripts && cd shell_scripts

Create a file in the directory by the name logical.sh as shown below

touch logical.sh

Make the file executable using the following command

chmod +x logical.sh

Open the file using nano as shown below

nano logical.sh

Once nano has opened the file, enter the following at the top of your shell file

#!/usr/bin/bash

This line allows your file to be run using bash. It should always be the first line in your script

We start by learning the logical operator AND. In order to do this, we will start by reading user input and create a program that will use IF statements to write conditions using the AND operator and print conditional output on our terminal.

Edit your shell script as shown below

#!/usr/bin/bash

# Capture user input for age and store it in a variable AGE
read -p "What is your age?: " AGE

# Condition to ensure that age is not empty. Do this using the -z syntax
if [[ -z $AGE ]]
then
echo "Error: No value provided for age"
exit 1
fi

# Condition to ensure that age is a number
if ! [[ $AGE =~ ^[0-9]+$ ]]
then
echo "Error: Expected age to be a number. Age can only be a number"
exit 1
fi

# Write your code for the AND operator below this comment

The AND operator may be represented as && or -a.

Lets now use it in our code as shown below

#!/usr/bin/bash

# Capture user input for age and store it in a variable AGE
read -p "What is your age?: " AGE

# Condition to ensure that age is not empty. Do this using the -z syntax
if [[ -z $AGE ]]
then
echo "Error: No value provided for age"
exit 1
fi

# Condition to ensure that age is a number
if ! [[ $AGE =~ ^[0-9]+$ ]]
then
echo "Error: Expected age to be a number. Age can only be a number"
exit 1
fi

# Write your code for the AND operator below this comment
if [[ $AGE -ge 0 && $AGE -le 17 ]]
then
echo "You are young"
elif [[ $AGE -ge 18 && $AGE -le 35 ]]
then
echo "You are a youth"
else
echo "You are old"
fi

exit 0

In the above script, we have used the && operator to check if the AGE is greater than or equal to 0 and less than or equal to 17. If this condition is satisfied, we print “You are young” on the terminal. We also check if AGE is greater than or equal to 18 and less than or equal to 35 we print “You are a youth” on the terminal. When the age is greater than 35 we print “You are old” on the terminal.

Save your file and exit the nano using the following commands. ctrl + O then Enter then ctrl + x

Run your script as shown below

~$ ./logical.sh
What is your age?: 23
You are a youth

Try out with different values for age to see different values

We can now look at the OR operator. The OR operator is represented as || or -o

We will add the script for our OR operator at the bottom of our file as shown below

#!/usr/bin/bash

# Capture user input for age and store it in a variable AGE
read -p "What is your age?: " AGE

# Condition to ensure that age is not empty. Do this using the -z syntax
if [[ -z $AGE ]]
then
echo "Error: No value provided for age"
exit 1
fi

# Condition to ensure that age is a number
if ! [[ $AGE =~ ^[0-9]+$ ]]
then
echo "Error: Expected age to be a number. Age can only be a number"
exit 1
fi

# Write your code for the AND operator below this comment
if [[ $AGE -ge 0 && $AGE -le 17 ]]
then
echo "You are young"
elif [[ $AGE -ge 18 && $AGE -le 35 ]]
then
echo "You are a youth"
else
echo "You are old"
fi

# Code for the OR operater
if [[ $AGE == 17 || $AGE == 35 ]]
then
echo "You are about to transition from one age bracket to another"
fi

exit 0

Save and run the file to test the funcionality.

Let us now combine the two — the AND and the OR operators in one condition.

We can do this using another example. In this example we verify that a user can log in to the terminal using the correct username and password.

Create a new file, name it auth.sh and make it executable as shown below

touch auth.sh && chmod +x auth.sh

Add the following script to create the program

#!/usr/bin/bash

# Create a dummy username and password that you can verify against
SAVED_USERNAME="johndoe"
SAVED_PASSWORD="password"

read -p "Enter your username: " USERNAME

if [[ -z $USERNAME ]]
then
echo "Error: Username cannot be empty"
exit 1
fi

read -sp "Enter password: " PASSWORD
echo

if [[ -z $PASSWORD ]]
then
echo "Error: Password cannot be empty"
exit 1
fi

if [[ $USERNAME == $SAVED_USERNAME && $PASSWORD == $SAVED_PASSWORD ]]
then
echo "Logged in successfully!"
elif [[ $USERNAME == $SAVED_USERNAME && $PASSWORD != $SAVED_PASSWORD || $PASSWORD == $SAVED_PASSWORD && $USERNAME != $SAVED_USERNAME ]]
then
echo "Invalid credentials, Username and Password did not match"
fi

exit 0

Save the file and exit the nano.

Run the file and try logging in with different sets of credentials

Congratulations! If you have any questions you can ask them in the chat box below.

--

--