Bash Scripting- 5 — Performing FILE, STRING comparisons & NUMERIC comparisons in Bash — The Linux GURUS

Shujat Husain
2 min readJan 13, 2020

--

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,

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.

echo “$dir is a directory “

echo “$dir is not a directory”

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,

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,

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,

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,

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:-

TheLinuxGURUS are thankful for your continued support.

Originally published at https://thelinuxgurus.com on January 13, 2020.

--

--

Shujat Husain
Shujat Husain

Written by Shujat Husain

A TechEenthusiat who likes all new technologies & Gaming. Most of all love to write about Cloud & DevOPs & have my own blog as well (https://linuxtechlab.com).

No responses yet