Welcome! In this lesson, we will explore the powerful trap
command in Bash. The trap
command executes a set of commands when a script exits, whether it completes successfully or encounters an error. This command is crucial for managing cleanup operations in your scripts. Proper cleanup ensures that temporary files and other resources do not clutter your system or cause unexpected behaviors.
Let's get started!
The trap
command in Bash allows you to specify commands that should be executed when a certain signal is received by the script. Signals are notifications sent to a process to notify it of various events. For example, the EXIT
signal is sent when the script exits, either normally or due to an error. The INT
(interrupt) signal triggers when you press Ctrl+C
to interrupt the script.
The general syntax for the trap
command in Bash is as follows:
This line instructs the script to run commands
when the specified signal
is received. commands
can be a string of commands, another script or a function.
The following script creates a file called temp.txt
then exits. We use the trap
command to remove this temporary file once the script exits.
solution.sh
In this script:
- The function removes temporary files and prints output before and after .
The trap
command can handle various signals, not just EXIT
. For instance, the INT
signal is sent when you press Ctrl+C
to interrupt a script. Let’s modify our script to handle the INT
signal as well. This script sets up two cleanup functions: one for handling the EXIT
signal and another for the INT
signal. When the script exits normally, the cleanup_exit
function is invoked. If the user interrupts the script by pressing Ctrl+C
, the cleanup_int
function prints a message indicating that the script was interrupted. However, it does not remove the temporary file.
In the script:
cleanup_exit
defines a function that prints a descriptive message and removestemp.txt
cleanup_int
defines a function that prints a descriptive message but does not remove
We can also specify what commands to run based on the exit status. This script sets up two cleanup functions to handle success (cleanup_success
) and failure (cleanup_failure
), and a general cleanup function (cleanup
) to dispatch the appropriate cleanup based on the exit code. It attempts to read a file file.txt
. Depending on if cat file.txt
succeeds or fails, the script exits with the appropriate status, triggering the relevant cleanup function.
-
The
cleanup_success
function prints a success message then callsrm -f fail.txt
to removefail.txt
-
The
cleanup_failure
function prints a failure message then calls to remove
Great job! In this lesson, you learned how to:
- Define cleanup functions to manage resources.
- Use the
trap
command to execute cleanup functions on script exit. - Handle various signals like
EXIT
andINT
with specific cleanup commands. - Manage multiple exit statuses using the
trap
command to ensure proper cleanup for successful and failed script executions.
Now it's time to put your new knowledge into practice! Head over to the practice section to try these concepts on your own. By practicing, you'll gain confidence and become adept at writing scripts that handle resources efficiently. Happy scripting!
