🔧Error Handling in Shell Scripting .
Learn to write robust shell scripts by understanding exit statuses, traps, and custom error messages.
Table of contents
Handling errors is like having a safety net for your shell scripts! 🎯 Today, you’ll learn how to catch mistakes, clean up when things go wrong, and display meaningful messages. Let’s make scripting simple and reliable! 🚀
What You’ll Learn 📚
Exit Status (
$?
): Know if a command succeeded or failed.✅
0
: Success❌ Non-zero: Failure
Error Handling with
if
: Check for errors at every step.Cleanup with
trap
: Automatically clean up temporary files and resources.Redirect Errors: Save error messages to files for debugging.
Custom Messages: Provide user-friendly error explanations.
Code Examples and Explanation 🛠️
Task 1 : Checking Exit Status 🔍
#!/bin/bash
mkdir /tmp/mydir
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to create directory /tmp/mydir."
else
echo "✅ Success: Directory /tmp/mydir created."
fi
Detailed Explanation:
mkdir /tmp/mydir
: Attempts to create a directory.$?
: Captures the exit status of the last command:0
: Success.Non-zero: Failure.
if [ $? -ne 0 ]
: Checks if the exit status is not equal to0
(failure).echo
: Prints a success or error message.
Task 2 : Using if
for Multiple Steps 🚦
#!/bin/bash
mkdir /tmp/mydir
if [ $? -ne 0 ]; then
echo "❌ Error: Could not create directory /tmp/mydir."
exit 1
fi
touch /tmp/mydir/myfile.txt
if [ $? -ne 0 ]; then
echo "❌ Error: Failed to create file myfile.txt in /tmp/mydir."
else
echo "✅ Success: File myfile.txt created."
fi
Detailed Explanation:
mkdir /tmp/mydir
: Creates a directory.if [ $? -ne 0 ]
: Checks if creating the directory failed.exit 1
: Exits the script with a non-zero status if an error occurs.touch /tmp/mydir/myfile.txt
: Creates a file in the new directory.if [ $? -ne 0 ]
: Checks if file creation failed.Error or success messages are printed based on the status.
Task 3 : Using trap
for Cleanup 🧹
#!/bin/bash
tempfile=$(mktemp)
trap "rm -f $tempfile; echo '🧹 Temporary file deleted.'" EXIT
echo "Temporary file created: $tempfile"
echo "Some data" > $tempfile
# Simulate an unexpected error
exit 1
Detailed Explanation:
tempfile=$(mktemp)
: Creates a unique temporary file and stores its name in the variabletempfile
.trap "rm -f $tempfile; echo '🧹 Temporary file deleted.'" EXIT
:- Sets up a "trap" to delete the temporary file when the script exits (even if it exits unexpectedly).
echo "Some data" > $tempfile
: Writes some data to the temporary file.exit 1
: Simulates an unexpected error and triggers thetrap
to delete the file.
Task 4 : Redirecting Errors ➡️
#!/bin/bash
cat non_existent_file.txt 2> error.log
echo "Error messages (if any) have been saved to error.log."
Detailed Explanation:
cat non_existent_file.txt
: Tries to display the contents of a file that doesn’t exist.2> error.log
: Redirects the error message (2
is for standard error) to a file namederror.log
.echo
: Notifies the user that errors are logged inerror.log
.
Task 5 : Creating Custom Error Messages ✍️
#!/bin/bash
mkdir /tmp/mydir
if [ $? -ne 0 ]; then
echo "❌ Error: Could not create /tmp/mydir.
Please check your permissions or if the directory already exists."
else
echo "✅ Directory created successfully!"
fi
Detailed Explanation:
Command:
mkdir /tmp/mydir
- This attempts to create a directory named
mydir
in the/tmp
folder.
- This attempts to create a directory named
Exit Status Check:
if [ $? -ne 0 ]
$?
holds the exit status of the previous command (mkdir
in this case).If the status is not equal to 0 (
-ne 0
), the command failed.
Error Message:
echo "❌ Error: Could not create /tmp/mydir. Please check..."
- If the directory couldn’t be created, it prints an error message suggesting possible reasons (e.g., no permissions or the directory already exists).
Success Message:
echo "✅ Success: Directory /tmp/mydir created."
- If the directory is successfully created, the script prints a success message.
Without proper error handling, scripts can fail silently, leaving behind messy files and incomplete tasks. With these techniques, you can create scripts that are reliable, maintainable, and user-friendly 💡.