SEARCH

How-To Geek

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

banner-01

We’ve covered enough of the basics in our guide on shell scripting that you should feel comfortable experimenting. In this week’s installment, we’ll be tackling some of the more fun stuff, like conditions and “if-then” statements.

What Are Conditions?

In everyday language, we say that conditions are requirements that must be met for something to occur. For my laptop to be able to connect to the internet, there are several conditions that must be met, like having an ISP, the modem and/or router being on, my laptop being on, etc. It’s pretty simple, and if any one of those requirements are not met, the result – my laptop connecting to the internet – does not happen.

Conditions in the realm of computing work similarly. We can test whether a string matches another string, whether it doesn’t match another string, or even if it exists at all. Similarly, we can test numerical arguments to see if one is great than, less than, or equal to another. To get something to happen after the conditions of the test are met, we use “if-then” statements. Their format is pretty simple.

if CONDITION
then
command1
command2

commandn
fi

If Statements

Let’s run a quick little test script, shall we?

if test $1 -gt $2
then
echo “$1 is greater than $2″
fi

test gt

You’ll notice that only when that condition is true will the script execute the following command. Otherwise, the “if” statement will exit. If there are any commands after the “if” statement, then they will be run as normal. I added the following line to the end of our above script to illustrate this:

echo “This comes after the if statement”

command post-if

Here are some other numerical operators you may want to try out:

  • -eq: equal to
  • -ne: not equal to
  • -lt: less than
  • -le: less than or equal to
  • -gt: greater than
  • -ge: greater than or equal to

Testing Strings

Now, if we modify the first line of our script to be this:

if test $1 = $2

then the condition will test if the two are equal. There’s a catch here though!! The use of an equals sign (=) compares two strings, and not numbers. If you wish to compare numbers, you’d need to use the “-eq” operator similarly to how we used “-gt” above.

comparing strings

Now, let’s make another modification:

if test $1 != $2

comparing strings wrongly

The inclusion of the exclamation point (!) acts as a “not” modifier. That is, it only runs the following command when the two strings do not match.

Here’s a list of some more string-based tests you can use:

  • string: using just an argument by itself tests if the string is not blank (null) or not defined in some way
  • -n string: this will test if the string is not blank and is defined
  • -z string: this will test if the string is blank and is defined that way

What Else About If?

I’ll admit, that section title was definitely a bad pun. Okay, we know how to execute a command if a test is true, but what if we want to execute a different command if it is false? We can easily put the two together by adding a section to our “if-then” statements – an “else”!

if CONDITION
then
command1
command2

commandn
else
command1
command2

commandn
fi

Let’s put together a simple script.

ifthenelse

There’s everything with the proper indentation. If you look closely, you’ll notice that we used square brackets ( [ and ] ) instead of the test command. They’re functionally equivalent for our purposes, and you’re much more likely to see the square brackets for various reasons, so we’ll use them from now on.

Here’s what the output will look like:

ifthenelse test

It’s that easy!

What Do I Do Now?

Now that you know how to use “if-then-else” statements, you can run scripts that can perform tests. For example, you can run a script that will calculate an md5 hash of a file and then compare it to the one you downloaded in a file to see if they match.

For some bonus points, you can create a script that has a “for” loop, but uses test conditions instead of reading lines out of a list file…

 


We’re getting to some of the more interesting parts in our Beginner’s Guide to Shell Scripting. If you missed the previous lessons, here’s a quick list for you to check out:

 

  1. The Basics of Shell Scripting
  2. Using For Loops
  3. More Basic Commands
  4. What are the differences between Linux shells?
  5. How to use Basic Regular Expressions

If you’ve made or used scripts that utilize testing conditions, if-then-else statements, and “for” loops, share with us down in the comments!

Yatri Trivedi is a monk-like geek. When he's not overdosing on meditation and geek news of all kinds, he's hacking and tweaking something, often while mumbling in 4 or 5 other languages.

  • Published 08/18/11

Enter Your Email Here to Get Access for Free:

Go check your email!