Day 11 Task: Error Handling in Shell Scripting 🚦🛠️
In this task, we will explore error handling in shell scripting to make your scripts more robust and reliable. Error handling ensures that your scripts handle unexpected situations gracefully without crashing or leaving things in an inconsistent state.
- Understanding Exit Status: 🎯
Every command in Linux gives back a status code after it runs:
0 means it worked perfectly! 🎉
Non-zero (like 1, 2, etc.) means something went wrong. ❌
- Using
if
Statements for Error Checking: 🛡️
You can use if
statements to check if a command was successful or if it failed. 💥 This helps your script respond to errors and avoid problems!
- Using
trap
for Cleanup: 🧹
With the trap
command, you can catch unexpected errors or interruptions (like pressing Ctrl+C
🛑) and make sure your script cleans up properly!
- Redirecting Errors: 📄➡️🚫
You can redirect errors from your commands to keep your script clean and organized! 🧹
Send errors to a file to review them later 📄➡️
error.log
.Or, if you don't want to see the errors, send them to
/dev/null
to hide them completely! 🚫
- Creating Custom Error Messages: 🛠️💬
You can create custom error messages to make it easier to understand what went wrong in your script! 🚨
Task 1: Checking Exit Status
- Write a script that attempts to create a directory and checks if the command was successful. If not, print an error message.
Answer:
#!/bin/bash
# Create a directory
mkdir /home/ubuntu/Task1
# Check if a directory is created or not
if [ $? -ne 0 ]; then
echo "Directory creation failed /home/ubuntu/Task1"
else
echo "Directory successfully created at /home/ubuntu/Task1"
fi
Task 2: Using if Statements for Error Checking
- Modify the script from Task 1 to include more commands (e.g., creating a file inside the directory) and use if statements to handle errors at each step.
Answer:
#!/bin/bash
# Create a directory
mkdir /home/ubuntu/Task2
#Check if a directory is created or not
if [ $? -ne 0 ]; then
echo "Directory creation failed /home/ubuntu/Task2" exit 1
fi
# Create if file created inside directory
touch /home/ubuntu/Task2/testfile.txt
#Check if a file is created or not
if [ $? -ne 0 ]; then
echo "file creation failed /home/ubuntu/Task2/testfile.txt" exit 1
fi
echo "Directory & file created successfully"
Task 3: Using trap for Cleanup
- Write a script that creates a temporary file and sets a trap to delete the file if the script exits unexpectedly.
Answer:
#!/bin/bash
tempfile="/home/ubuntu/Task3/tempfile$$.txt"
touch $tempfile
echo "Temporary file $tempfile created"
# Set a trap to remove the temporary file on script exit (normal or unexpected)
trap 'rm -f "$tempfile"' EXIT
echo "Deleting Temp file"
#Simulating with sleep
echo "Script is running..."
sleep 20
# Script normal exit
echo "Script ran successfully"
Task 4: Redirecting Errors
- Write a script that tries to read a non-existent file and redirects the error message to a file called error.log.
Answer:
#!/bin/bash
# Redirect the error message to a file called error.log
cat non_exiting_file.txt 2> error.log
Task 5: Creating Custom Error Messages
- Modify one of the previous scripts to include custom error messages that provide more context about what went wrong.
Answer:
#!/bin/bash
# Create a directory
mkdir /home/ubuntu/Task5/testdir
#Check if a directory is created or not
if [ $? -ne 0 ]; then
echo "Directory creation failed /home/ubuntu/Task1 Check if you have required rights to create a directory"
fi