O'Reilly logo

Linux Shell Scripting Cookbook by Sarath Lakshman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Comparisons and tests

Flow control in a program is handled by comparison and test statements. Bash also comes with several options to perform tests that are compatible with the UNIX system-level features.

Getting ready

We can use if, if else, and logical operators to perform tests and certain comparison operators to compare data items. There is also a command called test available to perform tests. Let's see how to use those commands.

How to do it...

If condition:

if condition;
then
commands;
fi

else if and else:

if condition; 
then
commands;
elif condition; 
then
    commands
else
    commands
fi

Nesting is also possible with if and else. if conditions can be lengthy. We can use logical operators to make them shorter as follows:

[ condition ] && action; # action ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required