Last active
August 11, 2023 08:54
-
-
Save harishkannarao/9aeadd464cc3926413df27915ed77d55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Make the script to abort if any command fails. Use set +e to change the behaviour and ignore failed command. | |
set -e | |
# Uncomment below line to print the commands as it is executed. Useful for debugging. Use set +x to disable debugging. | |
# set -x | |
echo "Example of numerical comparison with if elif else" | |
for (( c=-5; c<=5; c=$c+1 )) | |
do | |
if [[ $c -lt -4 ]] | |
then | |
echo "$c is less than -4" | |
elif [[ ($c -ge -4) && ($c -le -1) ]] | |
then | |
echo "$c is greater than or equal to -4 and less than or equal to -1" | |
elif [[ $c -eq 0 ]] | |
then | |
echo "$c is equal to 0" | |
elif [[ ($c -ge 1) && ($c -le 4) ]] | |
then | |
echo "$c is greater than or equal to 1 and less than or equal to 4" | |
elif [[ $c -gt 4 ]] | |
then | |
echo "$c is greater than 4" | |
fi | |
done | |
echo "Example of string comparison" | |
if [[ "ab" == "ab" ]] | |
then | |
echo "'ab' is equal to 'ab'" | |
fi | |
if [[ "ab" != "ba" ]] | |
then | |
echo "'ab' is not equal to 'ba'" | |
fi | |
REGEX='^ab.*$' | |
if [[ "abc" =~ $REGEX ]] | |
then | |
echo "'abc' matches regex '^ab.*$'" | |
fi | |
if [[ ! ("bac" =~ $REGEX) ]] | |
then | |
echo "'bac' doesn't match regex '^ab.*$'" | |
fi | |
echo "Example of and (&&) or (||) not (!)" | |
if [[ (2 -gt 0) && (2 -gt 1) ]] | |
then | |
echo "2 is greater than 0 and 1" | |
fi | |
if [[ (2 -gt 3) || (2 -gt 1) ]] | |
then | |
echo "2 is not greater than 3 but 2 is greater than 1" | |
fi | |
if [[ (! (2 -gt 3) ) && (! (2 -gt 4) ) ]] | |
then | |
echo "2 is not greater than 3 and 2 is not greater than 4" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment