DEV Community

Kanav Gathe
Kanav Gathe

Posted on • Edited on

Day 11/90: Error Handling in Shell Scripts 🛡️ #90DaysOfDevOps

Day 11: Error Handling in Shell Scripts 🚀

Hello DevOps enthusiasts! 👋 Welcome to Day 11 of the #90DaysOfDevOps challenge. Today, we're diving into error handling in shell scripts.

Task Solutions 💻

1. Exit Status Checking

#!/bin/bash

# Create directory and check status
mkdir testdir
if [ $? -ne 0 ]; then
    echo "Failed to create directory"
    exit 1
fi
echo "Directory created successfully"
Enter fullscreen mode Exit fullscreen mode

2. If Statement Error Checking

#!/bin/bash

# Multiple commands with error checking
if mkdir -p testdir; then
    if touch testdir/testfile; then
        echo "File created successfully"
    else
        echo "Failed to create file"
        exit 1
    fi
else
    echo "Failed to create directory"
    exit 1
fi
Enter fullscreen mode Exit fullscreen mode

3. Using Trap for Cleanup

#!/bin/bash

# Create temporary file
temp_file=$(mktemp)

# Set trap for cleanup
trap 'rm -f "$temp_file"' EXIT

echo "Working with temporary file"
# Script exits and temp file is removed automatically
Enter fullscreen mode Exit fullscreen mode

4. Error Redirection

#!/bin/bash

# Redirect errors to file
cat nonexistent_file.txt 2> error.log
Enter fullscreen mode Exit fullscreen mode

5. Custom Error Messages

#!/bin/bash

process_file() {
    if [ ! -f "$1" ]; then
        echo "Error: File $1 does not exist"
        exit 1
    fi
    echo "Processing file $1"
}
Enter fullscreen mode Exit fullscreen mode

Key Takeaways 💡

  • Exit status checks prevent cascading failures
  • Traps ensure cleanup on script exit
  • Error redirection helps in logging
  • Custom messages improve debugging
  • Proper error handling is crucial for reliability

Bash #DevOps #ErrorHandling #Linux #90DaysOfDevOps


This is Day 11 of my #90DaysOfDevOps journey. Keep handling those errors!

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay