Day 11 : Error Handling in Shell Scripting

Day 11 : Error Handling in Shell Scripting

Learning Objectives

Understanding how to handle errors in shell scripts is crucial for creating robust and reliable scripts. Today, you'll learn how to use various techniques to handle errors effectively in your Bash scripts.

Topics to Cover

  1. Understanding Exit Status: Every command returns an exit status (0 for success and non-zero for failure). Learn how to check and use exit statuses.

  2. Using if Statements for Error Checking: Learn how to use if statements to handle errors.

  3. Using trap for Cleanup: Understand how to use the trap command to handle unexpected errors and perform cleanup.

  4. Redirecting Errors: Learn how to redirect errors to a file or /dev/null.

  5. Creating Custom Error Messages: Understand how to create meaningful error messages for debugging and user information.

Real-life Example

Imagine you are an engineer managing a large-scale application. You need to automate the deployment process. If one step in the process fails and you don’t handle the error, it could leave your application in an inconsistent state. Proper error handling ensures that you can detect, respond to, and recover from errors smoothly.

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.

Script:

#!/bin/bash
mkdir /tmp/mydir
if [ $? -ne 0 ]; then
  echo "Failed to create directory /tmp/mydir"
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.

Script:

#!/bin/bash
mkdir /tmp/mydir
if [ $? -ne 0 ]; then
  echo "Failed to create directory /tmp/mydir"
  exit 1
fi

touch /tmp/mydir/myfile.txt
if [ $? -ne 0 ]; then
  echo "Failed to create file /tmp/mydir/myfile.txt"
  exit 1
fi

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.

Script:

#!/bin/bash
tempfile=$(mktemp)
trap "rm -f $tempfile" EXIT

echo "This is a temporary file." > $tempfile
cat $tempfile

# Simulate an error
exit 1

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.

Script:

#!/bin/bash
cat non_existent_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.

Script:

#!/bin/bash
mkdir /tmp/mydir
if [ $? -ne 0 ]; then
  echo "Error: Directory /tmp/mydir could not be created. Check if you have the necessary permissions."
  exit 1
fi

touch /tmp/mydir/myfile.txt
if [ $? -ne 0 ]; then
  echo "Error: File /tmp/mydir/myfile.txt could not be created. Ensure the directory exists and you have write permissions."
  exit 1
fi

Conclusion

Error handling in shell scripting is essential for creating reliable and robust scripts. By understanding and implementing exit statuses, if statements, traps, error redirection, and custom error messages, you can ensure that your scripts handle errors gracefully and provide useful feedback. This not only helps in debugging but also in maintaining a smooth and consistent workflow, ultimately leading to more reliable automation in your DevOps practices.

Learn More

Check out this YouTube tutorial for a deeper dive into shell scripting:

Shell Scripting Tutorial for Beginner By Shubham Londhe sir

Connect and Follow Me on Socials:

LINKDIN | GITHUB |TWITTER