Beginner’s Guide to Shell Scripting: Conditions & If-Then Statements

You can begin experimenting with common conditions like “if-then” statements used in shell scripting once you are completely familiar with the basics of scripting.

What Are Conditions?

In a layman’s language, condition is a pre-requisite that should be fulfilled before an event can take place. For example, if you wish to connect your system to the internet you should have met pre-requisites like acquiring an ISP, a switched on router/modem and a functional laptop/computer. There are other requirements to be met as well before you get connected to the internet. Suffice to say if any of these conditions are not met you will find it impossible to log into the internet.

In a programming environment, conditions have the same relevance as they do in everyday life. Using conditions it can be tested if several computing sequences match each other or not. It can also be found out if a particular sequence exists in duplicity or not. Also, number based debates can be tested to find if numerical sequences are greater than/ less than/ or equal to each other. To get results after testing, conditional statements like “if-then” are commonly used.

What are If Statements?

Let’s take a look at a simple example:

If test $5 –gt $9
echo “$5 is greater than $9”

In this example, the command will be executed only when the condition holds true. Since the condition is not true, the command will not be carried out though the “if” statement will remain. However, in the event that the “if” statement is followed by a command, it will be performed as usual.

Also Read

The Beginner’s Guide To Shell Scripting: More Basic Commands & Chains
Beginner’s Guide To Shell Scripting: The Basics

What are Testing Strings?

Taking the above example, let’s make changes in it so that the script reads:

if test $5 = $9

Now the condition will run a test check to see whether this statement is true or not. However, let’s not forget that the equal to sign (=) used here is meant for contrasting two strings instead of numbers.

Let’s look at some of the other string-based tests that can be made use of:

  • string: use of string-only helps test whether the string is not detailed; it also helps identify the non-blank status of the string (if at all).
  • -n string: use of this test will help find if the string is detailed and complete.
  • -z string: use of the test will identify if the string is non-complete and detailed.

How Else Can If be Used?

In the above examples we saw how to perform a command if the test is true. But what do we do with commands that have false statements?

You can simply carry out the two true and false commands by adding the “else” command along with “if-then”.
Once you are familiar and comfortable using the “if-then-else” statements, you can easily make use of scripts that are used for conducting test commands. A very simple example would be running a script that determines an md5 hash of a file. Once you have calculated this data you can make comparisons with the data of the downloaded file and see if the two match.

Anubha Pandey