0% found this document useful (0 votes)
9 views

How To Use Logical OR & AND in Shell Script With Examples - Unix

This document discusses how to use logical OR and AND operators in shell scripts. It provides examples of using these operators to check conditions and chain commands. It also includes a practical example of a script that takes a backup if a server is reachable and directory exists, demonstrating the use of these operators.

Uploaded by

phmeriahgmailcom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

How To Use Logical OR & AND in Shell Script With Examples - Unix

This document discusses how to use logical OR and AND operators in shell scripts. It provides examples of using these operators to check conditions and chain commands. It also includes a practical example of a script that takes a backup if a server is reachable and directory exists, demonstrating the use of these operators.

Uploaded by

phmeriahgmailcom
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

30/08/2023 19:50 How to Use Logical OR & AND in Shell Script with Examples

 

Start with Jira tem


Build a tool that adapts to how y

Jira

YOU ARE AT: Home » Bash Tips & Tricks » How to Use Logical OR & AND in Shell Script with Examples

How to Use Logical OR & AND in


Shell Script with Examples
By Rahul — April 18, 2023  4 Mins Read

Logical operators are used to combine multiple conditions in a script, allowing for more
complex decision-making. The two most commonly used logical operators in shell
scripting are Logical OR (||) and Logical AND (&&).

These operators are used in conditional statements like if, while, and until, as well as in
command chaining. Command chaining is a technique where multiple commands are
executed in a sequence, based on the success or failure of previous commands.

The Logical OR operator (||) is used to execute the command following it only if the
previous command fails (returns a non-zero exit status).
The Logical AND operator (&&) is used to execute the command following it only if
the previous command is successful (returns a zero exit status).

1. Using Logical OR (||) in Shell Scripts


Logical OR in bash script is used with operator -o. Write a small shell script that will
show how to use logical OR ( || ) operator between two conditions.
ADVERTISEMENT
Confidentialité - Conditions

https://tecadmin.net/use-logical-or-and-in-shell-script/ 1/5
30/08/2023 19:50 How to Use Logical OR & AND in Shell Script with Examples

#!/bin/bash

read -p "Enter First Numeric Value: " first


read -p "Enter Second Numeric Value: " second

if [ $first -le 10 ] || [ $second -gt 20 ]


then
echo "At least one conditions is true"
else
echo "Both conditions are failed"
fi

This shell script prompts the user to input two numeric values. It then checks if either the
first value is less than or equal to 10, or the second value is greater than 20. If at least
one of these conditions is met, the script outputs “At least one condition is true.”
Otherwise, it outputs “Both conditions are failed.”
ADVERTISEMENT

2. Using Logical AND (&&) in Shell Scripts


Logical AND in bash script is used with operator -a. Below shell script will show you to
how to use logical AND ( && ) between two conditions.

#!/bin/bash

read -p "Enter First Numeric Value: " first


read -p "Enter Second Numeric Value: " second

if [ $first -le 10 ] && [ $second -gt 20 ]


then
echo "Both conditions are true"
else
echo "Atleast one conditions is false"
fi

ADVERTISEMENT

https://tecadmin.net/use-logical-or-and-in-shell-script/ 2/5
30/08/2023 19:50 How to Use Logical OR & AND in Shell Script with Examples

This shell script prompts the user to input two numeric values. It then checks if the first
value is less than or equal to 10 and the second value is greater than 20. If both
conditions are met, the script outputs “Both conditions are true.” Otherwise, it outputs
“At least one condition is false.”

3. Using Multiple Logical OR & AND


Now, use the multiple logical operators in a single statement. The below example will
help you to understand how to use multiple logical operators in a single statement.

#!/bin/bash

# A sample shell script to take input a number from the user


# Check if the number is between 10 - 20
# Or number is between 100 - 200

read -p "Enter a number: " num

if ([ $num -ge 10 ] && [ $num -le 20 ]) || ([ $num -ge 100 ] && [ $num -le 200 ]
then
echo "Input number ($num) is between 10-20 or 100-200"
else
echo "Input number ($num) is neither between 10-20 nor 100-200"
fi

This shell script allows a user to input a number and then checks if the entered number
lies within either the range of 10-20 or 100-200. The script then outputs a message to the
user indicating whether the input number falls within one of these specified ranges or
not.

4. A Practical Example
In this practical example, we’ll create a shell script that checks if a server is reachable
and if a specific directory exists on the local machine. If both conditions are met, the
script will create a backup of the directory and transfer it to the remote server using scp.
If any of these conditions are not met, it will print an appropriate error message.

Create a file called backup_and_transfer.sh and add the following content:

#!/bin/bash

# Configuration
HOST="example.com"
DIRECTORY="/path/to/directory"

https://tecadmin.net/use-logical-or-and-in-shell-script/ 3/5
30/08/2023 19:50 How to Use Logical OR & AND in Shell Script with Examples

BACKUP_FILE="backup_$(date +%Y%m%d).tar.gz"
REMOTE_PATH="/remote/backup/directory"

# Check if the host is reachable and the local directory exists


ping -c 1 "$HOST" > /dev/null && [ -d "$DIRECTORY" ] && {

# Create a backup of the directory


tar -czf "$BACKUP_FILE" "$DIRECTORY" && {

# Transfer the backup file to the remote server


scp "$BACKUP_FILE" "$HOST":"$REMOTE_PATH" && {
echo "Backup and transfer completed successfully."
exit 0
} || {
echo "Failed to transfer the backup file."
exit 1
}

} || {
echo "Failed to create the backup file."
exit 1
}

} || {
echo "Host is not reachable or the local directory does not exist."
exit 1
}

This script demonstrates the use of Logical AND (&&) and Logical OR (||) operators to
chain commands, create backup files, and transfer them to a remote server, while
handling errors and providing useful feedback.

Conclusion
Mastering the Logical OR (||) and Logical AND (&&) operators in shell scripting enables
you to create efficient and effective scripts for managing systems and automating tasks.
By understanding their usage and combining them as needed, you can create complex
decision trees and streamline your shell scripts. Remember to follow best practices, such
as using parentheses to group conditions and leveraging short-circuit evaluation, to
optimize your scripts and ensure they work as intended. With practice and a thorough
understanding of these logical operators, you’ll be well on your way to becoming a shell
scripting expert.

bash Operator shell script


ADVERTISEMENT
https://tecadmin.net/use-logical-or-and-in-shell-script/ 4/5
30/08/2023 19:50 How to Use Logical OR & AND in Shell Script with Examples

      

RELATED POSTS

Assignment Operators in Bash

An Introduction to Bash Variables

Bash LOCAL and GLOBAL Variables

VIEW 5 COMMENTS

   

© 2023 Tecadmin.net. All Rights Reserved | Terms | Privacy Policy

https://tecadmin.net/use-logical-or-and-in-shell-script/ 5/5

You might also like