BASH Programming

Bash Error Handling

Error handling is a very important part of any programming language. Bash has no better option than other programming languages to handle the error of the script.  But it is essential to make the Bash script error-free at the time of executing the script from the terminal. The error handling feature can be implemented for the Bash script in multiple ways. The different techniques to handle the errors in the Bash script are shown in this tutorial.

Example 1: Error Handling Using a Conditional Statement

Create a Bash file with the following script that shows the use of the conditional statement for error handling. The first “if” statement is used to check the total number of command line arguments and print an error message if the value is less than 2. Next, the dividend and divisor values are taken from the command line arguments. If the divisor value is equal to 0, an error is generated and the error message is printed in the error.txt file. The second “if” command is used to check whether the error.txt file is empty or not. An error message is printed if the error.txt file is non-empty.

#!/bin/bash
#Check the argument values
if [ $# -lt 2 ]; then
   echo "One or more argument is missing."
   exit
fi
#Read the dividend value from the first command-line argument
dividend=$1
#Read the divisor value from the second command-line argument
divisor=$2
#Divide the dividend by the divisor
result=`echo "scale=2; $dividend/$divisor"|bc 2>error.txt`
#Read the content of the error file
content=`cat error.txt`
if [ -n "$content" ]; then
  #Print the error message if the error.txt is non-empty
  echo "Divisible by zero error occurred."
else
  #Print the result
  echo "$dividend/$divisor = $result"

 
Output:

The following output appears after executing the previous script without any argument:


The following output appears after executing the previous script with one argument value:


The following output appears after executing the previous script with two valid argument values:


The following output appears after executing the previous script with two argument values where the second argument is 0. The error message is printed:

Example 2: Error Handling Using the Exit Status Code

Create a Bash file with the following script that shows the use of the Bash error handling by exit status code. Any Bash command is taken as input value and that command is executed later. If the exit status code is not equal to zero, an error message is printed. Otherwise, a success message is printed.

#!/bin/bash

#Take a Linux command name
echo -n "Enter a command: "
read cmd_name
#Run the command
$cmd_name
#Check whether the command is valid or invalid
if [ $? -ne 0 ]; then
   echo "$cmd_name is an invalid command."
else
   echo "$cmd_name is a valid command."
fi

fi

 
Output:

The following output appears after executing the previous script with the valid command. Here, the “date” is taken as the command in the input value that is valid:


The following output appears after executing the previous script for the invalid command. Here, the “cmd” is taken as the command in the input value that is invalid:

Example 3: Stop the Execution on the First Error

Create a Bash file with the following script that shows the method to stop the execution when the first error of the script appears. Two invalid commands are used in the following script. So, two errors are generated. The script stops the execution after executing the first invalid command using the “set –e” command.

#!/bin/bash
#Set the option to terminate the script on the first error
set -e
echo 'Current date and time: '
#Valid command
date
echo 'Current working Directory: '
#Invalid command
cwd
echo 'login username: '
#Valid command
whoami
echo 'List of files and folders: '
#Invalid command
list

 
Output:

The following output appears after executing the previous script. The script stops the execution after executing the invalid command which is “cwd”:

Example 4: Stop the Execution for Uninitialized Variable

Create a Bash file with the following script that shows the method to stop the execution of the script for the uninitialized variable. The username and password values are taken from the command line argument values. If any of the values of these variables are uninitialized, an error message is printed. If both variables are initialized, the script checks if the username and password are valid or invalid.

#!/bin/bash
#Set the option to terminate the script for an uninitialized variable
set -u
#Set the first command-line argument value to the username
username=$1
#Set the second command-line argument value to the password
password=$2
#Check the username and password are valid or invalid
if [[ $username == 'admin' && $password == 'hidenseek' ]]; then
    echo "Valid user."
else
    echo "Invalid user."
fi

 
Output:

The following output appears if the script is executed without using any command-line argument value. The script stops the execution after getting the first uninitialized variable:


The following output appears if the script is executed with one command-line argument value. The script stops the execution after getting the second uninitialized variable:


The following output appears if the script is executed with two command-line argument values – “admin” and “hide”. Here, the username is valid but the password is invalid. So, the “Invalid user” message is printed:


The following output appears if the script is executed with two command-line argument values – “admin” and “hidenseek”. Here, the username and password are valid. So, the “Valid user” message is printed:

Conclusion

The different ways to handle the errors in the Bash script are shown in this tutorial using multiple examples. We hope that this will help the Bash users to implement the error-handling feature in their Bash script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.