Compare Numbers, Strings and Files in Bash Shell Script
IF / THEN in bash follow the same basic syntax:
Example
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,
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 VAL2VAL1 != 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.
We will now be creating another script that will use “-n” & “-z” with strings to check if they hold any value
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
No comments:
Post a Comment
Feel free to leave a comment! If you have any information that you think should be included, please do so here and I'll get it added in.