How To Use Logical OR & AND in Shell Script With Examples - Unix
How To Use Logical OR & AND in Shell Script With Examples - Unix
Jira
YOU ARE AT: Home » Bash Tips & Tricks » How to Use Logical OR & AND in Shell Script with Examples
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).
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
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
#!/bin/bash
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.”
#!/bin/bash
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.
#!/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"
} || {
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.
RELATED POSTS
VIEW 5 COMMENTS
https://tecadmin.net/use-logical-or-and-in-shell-script/ 5/5