How to create conditional structures in shell scripts using if statements

Conditional Structures

Simple shell scripts represent a collection of commands that are executed from beginning to end. Conditional structures allow users to incorporate decision making into shell scripts so that certain portions of the script are executed only when certain conditions are met.

Using the if/then Construct

The simplest of the conditional structures in Bash is the if/then construct, which has the following syntax.

if [CONDITION]; then
    [STATEMENT]
    ...
    [STATEMENT]
fi

With this construct, if a given condition is met, one or more actions are taken. If the given condition is not met, then no action is taken. The numeric, string, and file tests previously demonstrated are frequently utilized for testing the conditions in if/then statements. The fi statement at the end closes the if/then construct. The following code section demonstrates the use of an if/then construct to start the psacct service if it is not active

[user@host ~]$ systemctl is-active psacct > /dev/null 2>&1
[user@host ~]$ if  [ $? -ne 0 ]; then
> sudo systemctl start psacct
> fi

Using the if/then/else Construct

The if/then construct can be further expanded so that different sets of actions can be taken depending on whether a condition is met. This is accomplished with the if/then/else construct.

if [CONDITION; then
    [STATEMENT]
    ...
    [STATEMENT]
  else
    [STATEMENT]
    ...
    [STATEMENT]
fi

The following code section demonstrates the use of an if/then/else statement to start the psacct service if it is not active and to stop it if it is active.

[user@host ~]$ systemctl is-active psacct > /dev/null 2>&1
[user@host ~]$ if  [ $? -ne 0 ]; then
> sudo systemctl start psacct
> else
> sudo systemctl stop psacct
> fi

Using the if/then/elif/then/else Construct

Lastly, the if/then/else construct can be further expanded to test more than one condition, executing a different set of actions when a condition is met. The construct for this is shown in the following example:

if [CONDITION]; then
      [STATEMENT]
      ...
      [STATEMENT]
    elif [CONDITION]; then
      [STATEMENT]
      ...
      [STATEMENT]
    else      [STATEMENT]
      ...
      [STATEMENT]
fi

In this conditional structure, Bash tests the conditions in the order presented. When it finds a condition that is true, Bash executes the actions associated with the condition and then skips the remainder of the conditional structure. If none of the conditions are true, Bash executes the actions enumerated in the else clause.

The following code section demonstrates the use of an if/then/elif/then/else statement to run the MySQL client if the mariadb service is active, run the psql client if the postgresql service is active, or run the sqlite3 client if both the mariadb and postgresql services are not active.

[user@host ~]$ systemctl is-active mariadb > /dev/null 2>&1
MARIADB_ACTIVE=$?

[user@host ~]$ sudo systemctl is-active postgresql > /dev/null 2>&1
POSTGRESQL_ACTIVE=$?

[user@host ~]$ if  [ "$MARIADB_ACTIVE" -eq 0 ]; then
> mysql
> elif  [ "$POSTGRESQL_ACTIVE" -eq 0 ]; then
> psql
> else
> sqlite3
> fi