6- BASH Scripting – File, Numeric & String comparisons
HelloLinux Fanatics. In this tutorial, we will be discussing Numeric Comparisons, String Comparison & File Comparisons. (all scripting tutorials are here).
Syntax for doing 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
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
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
and lastly, we will use “-n” & “-z ” with strings to check if they hold any value,
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.
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.