Error Handling in Shell Scripting
This tutorial provides a detailed guide to handling errors in shell scripts, ensuring that your scripts can handle unexpected situations gracefully.
1. Understanding Exit Status
Every command in a shell script returns an exit status, a numeric value indicating success or failure. By convention, an exit status of 0 indicates success, while a non-zero value indicates failure.
To check the exit status of the last executed command, use $?
:
ls /nonexistent-directory
echo $?
This script attempts to list a nonexistent directory, which fails, and then prints the exit status of the ls
command, which will be non-zero.
2. Basic Error Handling
You can handle errors by checking the exit status of commands and taking appropriate actions. Here's an example:
#!/bin/bash
mkdir /some-directory
if [ $? -ne 0 ]; then
echo "Failed to create directory."
exit 1
fi
This script attempts to create a directory and checks if the command was successful. If it fails, it prints an error message and exits with a non-zero status.
3. Using set -e
The set -e
command can be used to make the script exit immediately if any command returns a non-zero exit status:
#!/bin/bash
set -e
mkdir /some-directory
echo "This message will only be printed if the directory was created successfully."
This script will exit immediately if the mkdir
command fails, skipping the subsequent echo
command.
4. Handling Errors with trap
The trap
command allows you to specify commands to be executed when the script exits or receives a signal. This is useful for cleaning up resources or logging errors.
4.1. Basic Usage of trap
Here's an example of using trap
to ensure a temporary file is deleted even if an error occurs:
#!/bin/bash
trap "rm -f /tmp/tempfile" EXIT
touch /tmp/tempfile
echo "Processing... (temporary file created)"
cp /nonexistent-file /tmp/tempfile
This script creates a temporary file and attempts to copy a nonexistent file to it, which will fail. The trap
command ensures the temporary file is deleted when the script exits, regardless of whether an error occurred.
4.2. Using trap for Error Logging
You can also use trap
to log errors:
#!/bin/bash
trap 'echo "Error occurred on line $LINENO"; exit 1' ERR
mkdir /some-directory
cp /nonexistent-file /some-directory
This script logs the line number where the error occurred and exits with a non-zero status.
5. Custom Error Messages
Providing informative error messages can help users understand what went wrong. Here's an example:
#!/bin/bash
file="/path/to/file"
if [ ! -e "$file" ]; then
echo "Error: File $file does not exist."
exit 1
fi
if [ ! -r "$file" ]; then
echo "Error: File $file is not readable."
exit 1
fi
This script checks if a file exists and is readable, providing custom error messages for each case.
6. Conclusion
In this tutorial, you learned various techniques for handling errors in shell scripts, including checking exit statuses, using set -e
, using trap
, and providing custom error messages. Effective error handling makes your scripts more robust and user-friendly, ensuring they can handle unexpected situations gracefully.