How to Compare Numbers, Strings and Files in Bash Shell Script

How to Compare Numbers, Strings and Files in Bash Shell Script

How to Compare Numbers, Strings and Files in Bash Shell Script : Hello Guys, In this tutorial, we will be discussing Numeric Comparisons, String Comparison & File Comparisons. (all scripting tutorials are here).

Syntax for doing comparisons

If [ conditions/comparisons]
then
commands
fi

We can use other conditional statements also with comparisons. Now, let’s start with

 

Numeric Comparisons

Firstly, before we write a script to do numeric comparisons we should know parameters involved . Below are the parameters used for numeric comparisons

  • n1 -eq n2      Checks if n1 is equal to n2
  • n1 -ge n2      Checks if n1 is greater than or equal to n2
  • n1 -gt n2       Checks if n1 is greater than n2
  • n1 -le n2       Checks if n1 is less than or equal to n2
  • n1 -lt n2       Checks if n1 is less than n2
  • n1 -ne n2     Checks if n1 is not equal to n2

Now lets write an example script

#!/bin/bash
#script to do numeric comparisons
num1=10
num2=20
if [ $num2 –gt $num1 ]
then
echo “$num2 is greater than $num1”
fi
#
If [ $num1 –gt 30]
then
echo “$num1 is greater than 30”
else
echo “$num1 is less than 30”
fi

So, that’s how we do numeric comparisons. Next we will discuss…..

 

Strings Comparisons

For doing strings comparisons, parameters used are

  • str1 = str2       Checks if str1 is the same as string str2
  • str1 != str2     Checks if str1 is not the same as str2
  • str1 < str2       Checks if str1 is less than str2
  • str1 > str2       Checks if str1 is greater than str2
  • -n str1               Checks if str1 has a length greater than zero
  • -z str1                Checks if str1 has a length of zero

Important Note :-  Notice that greater than symbol (>) & less than symbol (<) used here is also used for redirection in Linux.   So, if we are using any of these symbols in your scripts, they should be used escape character i.e. use it as “\>” or “\<”.

Example :- Firstly, we will be checking string equality, this script will check if username & our defined variables are same and will provide an output based on that

#!/bin/bash
# Script to do string equality comparison
name=dan
if [ $USER = $name ]
then
echo “User exists”
else
echo “User not found”
fi

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 less than small letter.

Let’s create a script to check these conditions

#! /bin/bash
# script to check string comparisons
str1=a
str2=z
str3=Z
if [ $str1 \> $str2 ]
then
echo “$str1 is greater”
else
echo “$str2 is greater”
fi

# Lower case/upper case scenario
If [ $str3 \> $str1]
then
echo “$str3 is greater”
else
echo “$str1 is greater”
fi

and lastly, we will use “-n” & “-z ” with strings to check if they hold any value,

#!/bin/bash
# Script to see if the variable holds any value
str1= “ ”
str2=world
if [ -n $str1 ]
then
echo “string not empty”
else
echo “string is empty”
fi

Same can done with “-z”, only difference being it searches for string with length zero while “-n” looks for value greater than zero.

 

File comparison

Parameters used for file comparison are

  • -d file                        Checks if file exists and is a directory
  • -e file                        Checks if file exists
  • -f file                         Checks if file exists and is a fi le
  • -r file                         Checks if file exists and is readable
  • -s file                        Checks if file exists and is not empty
  • -w file                       Checks if file exists and is writable
  • -x file                        Checks if file exists and is executable
  • -O file                       Checks if file exists and is owned by the current user
  • -G file                        Checks if file exists and the default group is the same as the current user
  • file1 -nt file2          Checks if file1 is newer than file2
  • file1 -ot file2          Checks if file1 is older than file2

These parameters are very useful and you will end up using these very frequently.

An example

#!/bin/bash
# Using file comparison
dir=/home/dan
if [ -d $dir ]
then
echo “$dir is a directory ”
cd $dir
ls –a
else
echo “$dir is not a directory”
fi

Same script with some modifications can used to check files also. You can yourself try these parameters to make a script , suitable to your needs.

This completes our tutorial. PRACTICE HARD TO LEARN SCRIPTING, create some scripts yourself as it will induce confidence in you.

And if having any doubts/queries, mention them below.

Was this Tutorial helpful? Help others share on Facebook, Twitter, and Google Plus!

 
Enjoyed this video?
How to Compare Numbers, Strings and Files in Bash Shell Script
"No Thanks. Please Close This Box!"