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]
We can use other conditional statements also with comparisons. Now, let’s start with
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
#script to do numeric comparisons
if [ $num2 –gt $num1 ]
echo “$num2 is greater than $num1”
If [ $num1 –gt 30]
echo “$num1 is greater than 30”
echo “$num1 is less than 30”
So, that’s how we do numeric comparisons. Next we will discuss…..
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
# Script to do string equality comparison
if [ $USER = $name ]
echo “User exists”
echo “User not found”
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
# script to check string comparisons
if [ $str1 \> $str2 ]
echo “$str1 is greater”
echo “$str2 is greater”
# Lower case/upper case scenario
If [ $str3 \> $str1]
echo “$str3 is greater”
echo “$str1 is greater”
and lastly, we will use “-n” & “-z ” with strings to check if they hold any value,
# Script to see if the variable holds any value
str1= “ ”
if [ -n $str1 ]
echo “string not empty”
echo “string is empty”
Same can done with “-z”, only difference being it searches for string with length zero while “-n” looks for value greater than zero.
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.
# Using file comparison
if [ -d $dir ]
echo “$dir is a directory ”
echo “$dir is not a directory”
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!