Day 11 : Error Handling in Shell Scripting

Hi, I’m Rakshita. A Cloud, DevOps, AI, and Python enthusiast passionate about learning and simplifying technology for others. I love exploring how modern tools and automation can make systems smarter and more efficient. Here, I write about: ☁️ Cloud & DevOps practices 🤖 AI in the world of automation 🐍 Python for real-world problem-solving 💡 Growth, consistency, and the learner’s mindset My goal is to bridge the gap between learning and doing, and help others grow confidently in the evolving tech landscape.
Tasks:
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.
Create a file:
vim create_dir.shEnter the script in file:
#!/bin/bash # Create directory mkdir /home/ubuntu/day11 # Check exit status if [ $? -ne 0 ]; then echo "Directory was not created" exit 1 fi echo "Directory created successfully"Make it executable and execute. The OUTPUT will be:

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.
#!/bin/bash
# Create directory inside directory
mkdir /home/ubuntu/day11/task2_dir
if [ $? -ne 0 ]; then
echo "Failed to create Directory /home/ubuntu/day11/task2_dir "
exit 1
fi
# Create file inside directory
touch /home/ubuntu/day11/task2_dir/newfile.txt
if [ $? -ne 0 ]; then
echo "Failed to create file /home/ubuntu/day11/task2_dir/newfile.txt"
exit 1
fi
echo "Directory and file created successfully /home/ubuntu/day11/task2_dir/newfile.txt"
OUTPUT:

Task 3: Using trap for Cleanup
Write a script that creates a temporary file and sets a
trapto delete the file if the script exits unexpectedly.#!/bin/bash # Create a temporary file tempfile="temp_file.txt" touch $tempfile # Trap to delete temporary file trap "rm -f $tempfile; echo 'Temporary file deleted'" EXIT # Stimualte with sleep (10 seconds) echo "Script running..." sleep 10 echo "Script completed successfully"OUTPUT: The script ran for 10 seconds and then temporary file deleted.

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.#!/bin/bash # Read non-existing file and redirect error cat non_existing_file 2> error.logOUTPUT:

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.
#!/bin/bash # Create directory mkdir /home/ubuntu/task5_dir if [ $? -eq 0 ]; then echo "Directory created successfully" else echo "Failed: Directory was not created for /home/ubuntu/task5_dir. Check if you have required permissions!" fi # Create a file inside directory touch /home/ubuntu/task5_dir/myfile5.txt if [ $? -eq 0 ]; then echo "File created successfully" else echo "Failed: File was not created for /home/ubuntu/task5_dir/myfile5.txt. Check, your directory already exist and the file has its required permissions! " fiOUTPUT: When the task successfully completed.

OUTPUT: When the task failed to complete.

HAPPY SCRIPTING🌟




