Autocodewizard Logo Error Handling and Debugging - Autocodewizard Ebooks - Bash Scripting Essentials

Chapter 10: Error Handling and Debugging

Understand how to handle errors gracefully, debug scripts, and troubleshoot issues with commands like set -e and trap.

In this chapter, we’ll explore methods for making scripts robust and reliable by handling errors and using debugging techniques. Error handling and debugging are essential skills for identifying issues in scripts and ensuring they run smoothly.

Using set -e for Error Handling

The set -e command causes a script to exit immediately if any command returns a non-zero exit status, preventing further errors:

# Example: Enabling set -e
set -e
echo "This will run"
false   # This command fails
echo "This will not run"

In this example, the script stops after false fails, skipping the final echo statement.

Using trap for Cleanup and Error Handling

The trap command allows you to execute a specified command when the script encounters an error, exits, or receives a signal, enabling cleanup or custom error handling:

# Example: Using trap for cleanup
trap 'echo "An error occurred. Exiting..."; exit 1' ERR
echo "This will run"
false   # This command fails
echo "This will not run"

In this example, trap captures any error and executes the specified command when an error occurs.

Debugging with set -x

The set -x command enables a debugging mode that prints each command to the terminal before executing it, helping you trace the script’s execution flow:

# Example: Enabling set -x
set -x
echo "This will print the command and result"
set +x
echo "This will not print the command"

The set +x command turns off debugging mode after set -x is enabled.

Using Exit Status for Error Checking

Each command in Bash returns an exit status, where 0 indicates success and any non-zero value indicates failure. You can use conditional statements to handle errors based on exit status:

# Example: Checking exit status
mkdir /some/directory
if [ $? -ne 0 ]; then
    echo "Failed to create directory"
fi

In this example, the if statement checks the exit status of mkdir and handles any errors by displaying a message.

Using trap with Signals

The trap command can also handle signals such as SIGINT (interrupt) or SIGTERM (terminate), allowing you to control how the script responds to user interruptions:

# Example: Handling interrupt signal
trap 'echo "Script interrupted"; exit' SIGINT
while true; do
    echo "Running... Press Ctrl+C to interrupt."
    sleep 2
done

This example uses trap to catch the SIGINT signal, displaying a message if the user presses Ctrl+C to interrupt the script.

Common Debugging Techniques

Additional tips for debugging scripts include:

# Example: set -u for unset variables
set -u
echo $undefined_variable  # This will trigger an error

Summary and Next Steps

In this chapter, we explored error handling and debugging techniques, including using set -e, trap, and set -x for script troubleshooting. Mastering these techniques will help you write more robust and error-resistant scripts. In the next chapter, we’ll look at advanced scripting techniques to further enhance your Bash scripting skills.