πŸš€Day 11: How to Handle Errors in Shell Scripting

πŸš€Day 11: How to Handle Errors in Shell Scripting

Β·

3 min read

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 πŸ“š

  1. How to check if a command worked. βœ…

  2. How to use if statements for error checking. πŸ”

  3. How to clean up using traps. 🧹

  4. How to redirect errors. πŸ”€

  5. 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>) to error.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

Β