Title: Bash Scripting- 5 – Performing FILE, STRING comparisons & NUMERIC comparisons in Bash
1Bash Scripting- 5 Performing FILE, STRING
comparisons NUMERIC comparisons in Bash
Posted by Prince DAN January 13, 2020 in DevOps
In this bash scripting tutorial, we will discuss
about performing file comparisons, String
comparisons as well as numeric comparisons. So
lets 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,
2File 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 lets 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
3then 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 lets see the numeric comparison in action
but first lets 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
lets see an example to demonstrate it,
4!/bin/bash num115 num230 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
lets 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 \lt str2
check if string 1 is less than string 2
str1 \gt str2
check if string 1 is greater than string 2
5-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
\lt \gt instead of using lt gt, 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 lets have look at some
examples to demonstrate the string
comparisons, !/bin/bash nameec2-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.
Lets see another example, ! /bin/bash str1b
str2y str3Y if str1 \gt str2 then echo
str1 is greater else
6echo str2 is greater fi If str3 \gt 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.
TheLinuxGURUS.com