Implementing Trap for Error Management

Tutorial 3 of 5

Introduction

The goal of this tutorial is to provide a comprehensive understanding of how to use the 'trap' command for error handling in shell scripts. This will allow you to create more robust, reliable scripts that can handle unexpected events gracefully.

By the end of this tutorial, you will learn:

  • What the 'trap' command is and how it works
  • How to catch signals and execute specific commands or functions on script exit

Prerequisites: Basic understanding of shell scripting is required.

Step-by-Step Guide

The 'trap' command is a function built into bash and other shells that allows you to catch signals and execute a specified command when the signal is received. The syntax of the 'trap' command is as follows:

trap command signal

Here, 'command' is the command or function to be executed when the signal is caught, and 'signal' is the signal to be caught.

The signals that can be caught by the 'trap' command include:

  • EXIT: This signal is sent when the script exits
  • ERR: This signal is sent when a command in the script fails
  • INT: This signal is sent when you press Ctrl+C
  • TERM: This signal is sent when the script is terminated

Code Examples

Let's look at an example of using the 'trap' command:

#!/bin/bash

# Define cleanup procedure
cleanup() {
    echo "Caught Interrupt ... cleaning up."
    # Add your cleanup code here
}

# Setup trap
# catch interrupt (ctrl+c) signal
trap "cleanup" INT

# Your script
while true
do
    echo "Sleeping ..."
    sleep 1
done

In this script, we define a cleanup function. We then set a trap to catch the INT (interrupt) signal. If you run this script and press Ctrl+C, it will catch the interrupt signal and run the cleanup function.

Summary

In this tutorial, we learned about the 'trap' command and how to use it for error handling in shell scripts. We also learned how to catch signals and execute specific commands or functions on script exit.

To learn more about error handling in shell scripts, you can refer to the man pages for the bash shell, or consult online resources.

Practice Exercises

  1. Write a shell script that sets a trap for the EXIT signal and prints a message when the script exits.

  2. Write a shell script that sets a trap for the ERR signal and prints a message when a command fails in the script.

  3. Modify the script from exercise 2 to execute a cleanup function when a command fails in the script.

Solutions:

  1. Here's a solution for exercise 1:

    ```bash

    !/bin/bash

    Setup trap

    trap "echo 'Script exiting...'" EXIT

    Your script

    echo "Hello, World!"
    ```

    This script will print "Script exiting..." when it exits.

  2. Here's a solution for exercise 2:

    ```bash

    !/bin/bash

    Setup trap

    trap "echo 'A command failed.'" ERR

    Your script

    echo "Hello, World!"
    false # This command will fail
    ```

    This script will print "A command failed." when the 'false' command fails.

  3. Here's a solution for exercise 3:

    ```bash

    !/bin/bash

    Define cleanup procedure

    cleanup() {
    echo "A command failed. Cleaning up..."
    # Add your cleanup code here
    }

    Setup trap

    trap "cleanup" ERR

    Your script

    echo "Hello, World!"
    false # This command will fail
    ```

    This script will run the cleanup function when the 'false' command fails.