Hey there! π Welcome to Day 11 of my 90 Days of DevOps journey. Today, weβre diving into something super important: Error Handling in Shell Scripting.
Objectives π
How to check if a command worked. β
How to use if statements for error checking. π
How to clean up using traps. π§Ή
How to redirect errors. π
How to create custom error messages. π’
Tasks π οΈ
Task 1: Checking Exit Status
Objective: Write a script that tries to create a directory and checks if it works. If not, print an error message.
Script:
#!/bin/bash
mkdir my_directory
if [ $? -eq 0 ]; then
echo "Directory created successfully! π"
else
echo "Failed to create directory. π"
fi
π Explanation:
mkdir my_directory
tries to create a folder named "my_directory".$?
checks if the last command worked (0
means yes, any other number means no).The
if
statement checks this and tells us if it succeeded or failed.
Task 2: Using if Statements for Error Checking
Objective: Modify the script from Task 1 to do more things (like creating a file inside the directory) and check for errors each step.
Script:
#!/bin/bash
mkdir my_directory
if [ $? -eq 0 ]; then
echo "Directory created successfully! π"
touch my_directory/my_file.txt
if [ $? -eq 0 ]; then
echo "File created successfully! π"
else
echo "Failed to create file. π"
fi
else
echo "Failed to create directory. π"
fi
π Explanation:
touch my_directory/my_file.txt
tries to create a file inside our new folder.Another
if
statement checks if this worked and prints a message.
Task 3: Using trap for Cleanup
Objective: Write a script that creates a temporary file and sets a trap to delete the file if the script stops unexpectedly.
Script:
#!/bin/bash
temp_file="temp.txt"
touch $temp_file
trap "rm -f $temp_file; echo 'Temporary file deleted. ποΈ'" EXIT
echo "Script running... π"
sleep 10 # Simulate a long process
echo "Script completed successfully! π"
π Explanation:
trap "rm -f $temp_file; echo 'Temporary file deleted. ποΈ'" EXIT
sets a trap that deletes our temp file when the script exits, even if something goes wrong.
Task 4: Redirecting Errors
Objective: Write a script that tries to read a non-existent file and sends the error message to a file called error.log.
Script:
#!/bin/bash
cat non_existent_file 2> error.log
echo "Tried to read a non-existent file. Check error.log for details. π"
π Explanation:
cat non_existent_file 2> error.log
tries to read a file that doesnβt exist and sends the error (2>
) toerror.log
.This way, the error message is saved in a file instead of cluttering your screen.
Task 5: Creating Custom Error Messages
Objective: Modify one of the previous scripts to include custom error messages that give more details about what went wrong.
Script:
#!/bin/bash
mkdir my_directory
if [ $? -eq 0 ]; then
echo "Directory created successfully! π"
touch my_directory/my_file.txt
if [ $? -eq 0 ]; then
echo "File created successfully! π"
else
echo "Error: Could not create file inside my_directory. Please check if you have write permissions. β"
fi
else
echo "Error: Could not create directory my_directory. Please check if the directory already exists or if you have the necessary permissions. β"
fi
π Explanation:
- Custom error messages give more details, making it easier to understand and fix issues.
Happy Learning!π
#DevOps #ShellScripting #ErrorHandling #90DaysOfDevOps