DEV Community

Cover image for Day 11 : Error Handling in Shell Scripting
Udoh Deborah
Udoh Deborah

Posted on

Day 11 : Error Handling in Shell Scripting

Hello everyone, Welcome to Day 11 of the 90 Days of DevOps journey. Today, we’re diving into: Error Handling in Shell Scripting.

Our objectives are to

  • Check if a command worked. ✅

  • Using if statements for error checking.

  • How to clean up using traps.

  • How to redirect errors.

  • Create custom error messages.

Step 1:
Launch terminal and navigate to your DevOps challenge project directory:

bash
cd ~/90daysofdevops/day11
Enter fullscreen mode Exit fullscreen mode

If the folder doesn't exist, create it:

bash
mkdir -p ~/90daysofdevops/day11
Enter fullscreen mode Exit fullscreen mode
 cd ~/90daysofdevops/day11

Enter fullscreen mode Exit fullscreen mode

✍️ Step 2: Create Script for Task 1
Create and open the file:

bash
nano task1_check_exit.sh
Enter fullscreen mode Exit fullscreen mode

Paste this code:

bash
#!/bin/bash

mkdir my_directory

if [[ $? -ne 0 ]]; then
  echo "Failed to create directory 'my_directory'"
else
  echo "Directory 'my_directory' created successfully"
fi
Enter fullscreen mode Exit fullscreen mode

Save and exit:

Step 3: Make the Script Executable and Run It

bash
chmod +x task1_check_exit.sh
./task1_check_exit.sh
Enter fullscreen mode Exit fullscreen mode

Step 4: we are going to Replicate this for the Remaining Tasks, with the scripts provided below.

🔹 Task 2: Using if Statements for Error Checking
Script: task2_if_check.sh

#!/bin/bash

dir_name="my_directory"

if mkdir "$dir_name"; then
  echo "✅ Directory '$dir_name' created."

  file_path="$dir_name/sample.txt"
  if touch "$file_path"; then
    echo "✅ File 'sample.txt' created inside '$dir_name'."
  else
    echo "❌ Failed to create file inside '$dir_name'."
  fi
else
  echo "❌ Could not create directory '$dir_name'."
fi


Enter fullscreen mode Exit fullscreen mode

🔹 Task 3: Using trap for Cleanup
Script: task3_trap_cleanup.sh

#!/bin/bash

temp_file="tempfile.txt"

# Cleanup function
cleanup() {
  echo "⚠️ Script interrupted. Cleaning up..."
  rm -f "$temp_file"
  echo "✅ Temporary file deleted."
}

# Trap on EXIT, INT (Ctrl+C), or TERM
trap cleanup EXIT INT TERM

# Simulate work
touch "$temp_file"
echo "Temporary file created: $temp_file"
sleep 10  # simulate processing
echo "Done."

Enter fullscreen mode Exit fullscreen mode

🔹 Task 4: Redirecting Errors
Script: task4_redirect_errors.sh

#!/bin/bash

cat missingfile.txt 2> error.log

echo "Check error.log for error output."

Enter fullscreen mode Exit fullscreen mode

🔹 Task 5: Creating Custom Error Messages
Script: task5_custom_error.sh

#!/bin/bash

filename="missingfile.txt"

if ! cat "$filename" 2> /dev/null; then
  echo "❌ Could not open '$filename'. Please make sure the file exists, and you have read permissions."
fi

Enter fullscreen mode Exit fullscreen mode
  • make sure to always make the script executable, save and then run the scripts.

Top comments (0)