If Statement

Learn how the if statement works in detail, along with one-liners and fragile code.

Conditional statements

We first learned of conditional statements the first time when we learned about the find utility. Then, we found out that Bash has its own logical operators AND (&&) and OR (||). Bash also has other options to make branches.

The operators if and case are used frequently when writing Bash scripts. These operators execute similar behavior. However, each of them fits better for some specific tasks.

If statement

Let’s imagine that we write a one-line command. Such a command is called a one-liner. We try to make it as compact as possible because a short command is faster to type. Also, shorter code gives us fewer chances of making a mistake when typing.

Now, let’s imagine that we write a script. The hard drive stores it. We call the script regularly and change it rarely. The code’s concision is not important in this case. Instead, we try to make the code simple to read and change.

The && and || operators fit well for one-liners. When we write scripts, Bash gives us better options. Actually, it depends on the particular case. Sometimes, we can use logical operators in the script and keep our code clean. However, in most cases, logical operators may lead to hard-to-read code. Therefore, it is better to replace them with the if and case statements.

Here is an example of a case when we would need an if statement. Let’s have a look at the following tar call from the backup script:

tar -cjf "$1".tar.bz2 "$@" &&
  echo "tar - OK" > results.txt ||
  { echo "tar - FAILS" > results.txt ; exit 1 ; }

When writing this script, we tried to make it easier to read. This motivation forced us to split the calls of the tar and mv utilities. This solution is still not enough. The tar call is too long and complicated for reading. Therefore, it’s easy to make a mistake when modifying it. Such error-prone code is called fragile code. We get it whenever we create a poor technical solution to a problem.

Improving the code

Let’s improve the tar call. The first step to improve the code is to write its algorithm cleanly. Here is the algorithm for our case:

  1. Read a list of files and directories from the $@ variable.
  2. Archive and compress the files and directories.
  3. If the archiving succeeds, write the “tar - OK” line into the log file.
  4. If an error occurrs, write the line “tar - FAILS” into the log file and terminate the script.

The last step is the most confusing one. When the tar succeeds, the script does one action only. When an error happens, there are two actions. These actions are combined into the single command block by curly brackets. This code block looks too complicated.

The if statement executes command blocks on specific conditions. The statement looks like this in the general form:

if CONDITION
then
  ACTION
fi

We can write the if statement in one line. To do that, we add semicolons before then and fi like this:

if CONDITION; then ACTION; fi

The condition and action refer to a single command or a block of commands. If the exit status of the condition equals zero, Bash executes the action.

Here is an example of the if statement. Let’s click the “Run” button to view the execution of if-smt.sh in two folders.

The folder named Same has two files with the same content while the Different folder has files with different content. Let’s observe the output of the execution of if-smt.sh in both folders.

Get hands-on with 1200+ tech skills courses.