Shell Scripting Error Handling

Introduction

Shell scripting error handling is an essential aspect of writing robust and reliable scripts in Linux. When writing shell scripts, it’s important to consider the various errors that could occur and how to handle them effectively. Errors can occur for a variety of reasons, such as incorrect user input, file permissions, network connectivity issues, and more. Without proper error handling, these errors can cause the script to fail or produce unexpected results.

In this tutorial, we’ll explore some common techniques of Shell scripting error handling, including how to use exit codes, how to capture error messages, and how to use conditional statements to handle errors. By the end of this tutorial, you’ll have a solid understanding of how to write robust and reliable shell scripts that can handle errors gracefully.

Checking Exit Status

The most common way to check for errors in a shell script is to check the exit status of a command. Every command in Linux returns an exit status code, which indicates whether the command was successful or not. A value of 0 indicates success, while any other value indicates an error.

To check the exit status of a command, use the $? variable. For example:

ls /nonexistent/path
if [ $? -ne 0 ]; then
    echo "Error: Directory not found."
fi

In this example, we run the ls command on a non-existent directory, which will return a non-zero exit status. We then check the $? variable and print an error message if the value is not equal to 0.

Setting Error Traps

The trap Command

The trap command allows you to specify a command or function that should be executed when a specific signal or error occurs. The most commonly used signal for error traps is ERR, which is triggered whenever a command returns a non-zero exit status.

Here’s an example of how to set an error trap using the trap command:

trap 'echo "Error: Script failed" >&2; exit 1' ERR

In this example, we’re setting an error trap that will echo an error message to standard error and exit the script with a non-zero exit status when an error occurs.

Using Functions for Error Traps

We can also use functions as error traps. This is useful if we have a lot of error handling code that we want to reuse in multiple places in our script.

Here’s an example of how to use a function as an error trap:

function cleanup {
    echo "Error: Script failed" >&2
    exit 1
}

trap cleanup ERR

In this example, we’re defining an cleanup function that echoes an error message to standard error and exits the script with a non-zero exit status. We’re then setting the ERR signal to trigger this function when an error occurs.

Removing Error Traps

If you want to remove an error trap, you can use the trap - command. For example:

trap - ERR

In this example, we’re removing the error trap for the ERR signal.

Exiting on Error

By default, a shell script will continue to run even if an error occurs. However, you can configure your script to exit immediately when an error occurs using the set command.

To exit immediately on error, use the following command:

set -e

You can also disable this behavior using the following command:

set +e

Printing Error Messages

Finally, it’s important to provide clear error messages when an error occurs in your script. This will help users understand what went wrong and how to fix the issue.

To print an error message in a shell script, use the echo command or a similar command. For example:

if [ ! -f "$filename" ]; then
    echo "Error: File not found: $filename"
    exit 1
fi

In this example, we check whether a file exists, and print an error message.

Also, see the example code shell-scripting-examples in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author