Tuesday, April 14, 2020

Bash Condition Comparisons

Compare Numbers, Strings and Files in Bash Shell Script

IF / THEN in bash follow the same basic syntax:
if [ conditions/comparisons]
    then
         commands
fi
Example
if [2 -gt 3]
     then
     print "2 is greater"
     else
     print "2 is not greater"
fi

Numeric Comparisons

This is one the most common evaluation method i.e. comparing two or more numbers. We will now create a script for doing numeric comparison, but before we do that we need to know the parameters that are used to compare numerical values . Below mentioned is the list of parameters used for numeric comparisons
VAL1 -eq VAL2 is VAL1 is equal to VAL2  
VAL1 -ge VAL2 is 
VAL1 is greater than or equal to VAL2  
VAL1 -gt VAL2 is 
VAL1 is greater than VAL2  
VAL1 -le VAL2 is 
VAL1 is less than or equal to VAL2  
VAL1 -lt VAL2 is 
VAL1 is less than VAL2
VAL1 -ne VAL2 is 
VAL1 is not equal to VAL2  
Now that we know all the parameters that are used for numeric comparisons, let’s use these in a script,
#!/bin/bash
# Script to do numeric comparisons
var1=10
var2=20
if [ $var2 -gt $var1 ]
    then
        echo "$var2 is greater than $var1"
fi
# Second comparison
If [ $var1 -gt 30]
    then
        echo "$var is greater than 30"
    else
        echo "$var1 is less than 30"
fi
This is the process to do numeric comparison, now let’s move onto string comparisons.

Strings Comparisons

When creating a bash script, we might also be required to compare two or more strings & comparing strings can be a little tricky. For doing strings comparisons, parameters used are
VAL1 VAL2 checks if VAL1 is the same as string VAL2
VAL1 != VAL2 checks if VAL1 is not the same as VAL2
VAL1 VAL2 checks if VAL1 is less than VAL2
VAL1 VAL2 checks if VAL1 is greater than VAL2
-n VAL1 checks if VAL1 has a length greater than zero
-z VAL1 checks if VAL1 has a length of zero
Note :-  You might have noticed that greater than symbol (>) & less than symbol (<) used here are also used for redirection for stdin or stdout in Linux. This can be a problem when these symbols are used in our scripts, so what can be done to address this issue.
Solution is simple , when using any of these symbols in scripts, they should be used with escape character i.e. use it as “/>” or “/<“.
Now let’s create a script doing the string comparisons.
In the script, we will firstly be checking string equality, this script will check if username & our defined variables are same and will provide an output based on that. Secondly, we will do greater than or less than comparison. In these cases, last alphabet i.e. z will be highest & alphabet a will be lowest when compared. And capital letters will be considered less than a small letter.
#!/bin/bash
# Script to do string equality comparison
name=linuxtechi
if [ $USER = $name ]
        then
                echo "User exists"
        else
                echo "User not found"
fi
# script to check string comparisons
var1=a
var2=z
var3=Z
if [ $var1 \> $var2 ]
        then
                echo "$var1 is greater"
        else
                echo "$var2 is greater"
fi
# Lower case  & upper case comparisons
if [ $var3 \> $var1 ]
        then
                echo "$var3 is greater"
        else
                echo "$var1 is greater"
fi
We will now be creating another script that will use “-n” & “-z” with strings to check if they hold any value
#!/bin/bash
# Script to see if the variable holds value or not
var1=" "
var2=linuxtechi
if [ -n $var1 ]
        then
                echo "string  is not empty"
        else
                echo "string provided is empty"
fi
Here we only used ‘-n’ parameter but we can also use “-z“. The only difference is that with ‘-z’, it searches for string with zero length while “-n” parameter searches for value that is greater than zero.

File comparison

This might be the most important function of comparison & is probably the most used than any other comparison. The Parameters that are used for file comparison are
-d file                     checks if the file exists and is it’s a directory
-e file                     checks if the file exists on system
-w file                   checks if the file exists on system and if it is writable
-r file                     checks if the file exists on system and it is readable
-s file                     checks if the file exists on system and it is not empty
-f file                     checks if the file exists on system and it is a file
-O file                   checks if the file exists on system and if it’s is owned by the current user
-G file                   checks if the file exists and the default group is the same as the current user
-x file                    checks if the file exists on system and is executable
file A -nt file B     checks if file A is newer than file B
file A -ot file B     checks if file A is older than file B
Here is a script using the file comparison
#!/bin/bash
# Script to check file comparison
dir=/home/linuxtechi
if [ -d $dir ]
        then
                echo "$dir is a directory"
                cd $dir
                ls -a
        else
                echo "$dir is not exist"
fi

No comments:

Post a Comment