In this bash scripting tutorial, we will discuss about performing file comparisons, String comparisons as well as numeric comparisons. So let’s start with file comparisons in bash scripts.

Recommended Read: Bash Scripting-6- Using Logical operators in shell scripts

Also Read: Important PostgreSQL commands that every beginner should know

File comparisons

The following are the parameters that can be used for file comparisons,

File Comparisons

-e file

check if file exists

-d file

check if it is a directory

-f file

check if it is a file

-r file

check if the file is readable

-s file

check if the file is not empty

-w file

check if the file is writable

-x file

check if the file is executable

-O file

check if the file is owned by the current user

-G file

check if the default group is the same as the current user

file1 -nt file2

check if file1 is newer than file2

file1 -ot file 2

check if file1 is older than file2

These comparisons parameters are one of the most frequently used parameters & you will end up using most of them on a regular basis. Now let’s example with these file comparisons in actions,

NOTE:- We write the comparative statements in brackets i.e. ‘[ ]’, whether its file, string or numeric comparisons.

#!/bin/bash

dir=/home/e2-user

if [ -d $dir ]

then

             echo “$dir is a directory ”

             cd $dir

             ls –a

else

             echo “$dir is not a directory”

fi

The above script is checking whether a directory exists & if does, it will print a message & will then also output the directory contents & if the directory does not exist then it will print a message saying that the directory does not exist.

Numeric comparisons

Now let’s see the numeric comparison in action but first let’s check the parameter that we can use for numeric comparisons,

Numeric Comparisons

n1 -eq n2

Checks if n1 is equal to n2

n1 -gt n2

checks if n1 is greater than n2

n1 -ge n2

checks if n1 is greater than or equal to 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 let’s see an example to demonstrate it,

#!/bin/bash

num1=15

num2=30

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

String Comparisons

Now is the time to discuss string comparisons, so let’s start with list of the parameters that are used for performing string comparisons,

String Comparisons

str1 = str2

check if string 1 is equal to string 2

str1 != str2

check if string 1 is not equal to string 2

str1 \< str2

check if string 1 is less than string 2

str1 \> str2

check if string 1 is greater than string 2

-n str1

check if the string 1 has a length greater than zero

-z str1

check if the string 1 has a length equal to zero

You might have noticed that we have used ‘\<’ & ‘\>’ instead of using ‘<’ & ‘>’, that is because both these symbols are used for redirection in Linux as well. So we have to use the escape symbol i.e. \. to use them for string comparisons.

Now let’s have look at some examples to demonstrate the string comparisons,

#!/bin/bash

name=”ec2-user”

if [ $USER = $name ]

then

             echo “User exists”

else

             echo “User not found”

fi

This is a simple script to check string comparisons, here we are checking a string i.e. a user name & checking it with provided name in the variable. Let’s see another example,

#! /bin/bash

str1=b

str2=y

str3=Y

if [ $str1 \> $str2 ]

then

             echo “$str1 is greater”

else

             echo “$str2 is greater”

fi

If [ $str3 \> $str1]

then

             echo “$str3 is greater”

else

             echo “$str1 is greater”

fi

Here we are comparing 3 strings with each other, we also compared a small letter with capital one. Keep in mind capital letters will be less than small letters & ‘z’ will be highest with ‘a’ as lowest letter.

We now end this tutorial on file, numeric & string comparisons. Please do send us any suggestions, queries or questions using the comment box below.

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter | Linkedin

TheLinuxGURUS are thankful for your continued support.