Skip to main content
Bash Scripting

Extending the BASH If Statement

By April 12, 2013September 12th, 2022No Comments

Extending the BASH IF statementUsing BASH IF Statements

Within the last lesson where we looked at using code snippets, we saw the use of a simple if statement. Although we will find IF statements useful by themselves they are often better served with the keywords ELSE and ELIF. Allowing us to test for more than one condition. So in this lesson, we will be extending the BASH IF statement and we can start looking at using else and “else if”, or, as is written, elif statements within our BASH scripts.

Simple IF

We can see in the screenshot included in the header of this page an example if statement in bash. We include the code below if you want yo copy it into your own editor.

if [ -e $1 ]
  then
    echo "The file $1 is there"
fi

Viewing the code we see we are testing for the single condition. The option e tests that a file exists. The file that we are testing is passed through when the script runs. The $1 variable represents the first argument to the script. The block of code that we execute simply prints to the screen again using the variable as the file name. Although this is good, it is far from perfect. One omission is that nothing happens if the file does not exist. You might say that the script fails silently which is never a good thing. There ways around this and we do not, necessarily need to add in any additional keywords to extend the bash if statement.

Reversing the IF Test

Testing for the reverse condition we can exit the script if the file does not exist.

if [ ! -e $1 ]
  then
    echo "The file $1 is not there"
    exit 2
fi
echo "The file $1 is there"

The exclamation mark is used to reverse the condition and the symbol represents the work NOT. We now only enter the code block for the IF statement if the file we are testing does not exist. We also exit  the script and so we will process no further code. The line exit 2 is used to leave the script with an error state. Anything other than an exit code of 0 is used to indicate an error state.

Using ELSE

Should we not wish to leave the script if the file is not present then we can make use of the ELSE keyword. We can modify the previous script example as in the following.

if [ -e $1 ]
  then
    echo "The file $1 is there"
  else
    echo "The file $1 is not there"
fi
echo "Finished"

As we now do not run the exit command if the file is not there we are free to execute further code. This allows the final echo statement to run no matter of the existence of the file.

Using ELIF

Whilst the keyword ELSE is very good it is still not perfect. It essentially extends the BASH IF statement into allowing one condition and everything else. Wishing more granularity in the testing then we may choose to use the ELIF keyword, meaning “ELSE IF”. In the following example, we look for the words directory, link and file being the value of the first argument passed through to the script. Based on one of those three words we run the find command with different options.

#!/bin/bash
#This file was created on 19/02/13

if [[ $1 = "directory" ]]
  then
     find /etc -maxdepth 1 -type d
 elif [[ $1 = "link" ]]
  then
     find /etc -maxdepth 1 -type l
 elif [[ $1 = "file" ]]
  then
     find /etc -maxdepth 1 -type f
 else
        echo "Usage: $0 file | directory | link "
fi

For each test we make we check the first input parameter to the script, $1, to see if it is “directory”, “link” or “file” before running the appropriate find command. If we do not match on anything then we echo out simple usage instructions. The variable $0 being the name of the script itself.

My Book on Shell Scripting is available from good bookshops and the publisher Packt.

Mastering Linux Shell Scripting by Andrew Mallett.jpg