Day 5:Error Handling in Shell Scripting and Git Basics Revision

Aditya TiwaryAditya Tiwary
4 min read

.


🟣 Error Handling in Shell Scripting β€” A Practical Guide πŸš€

πŸ“š Learning Objectives

Creating reliable and professional Bash scripts requires solid error-handling skills. In this guide, you'll learn:

  • How to handle errors effectively in Bash scripts.

  • Techniques to catch errors, provide meaningful messages, and clean up resources.


βœ… Why Error Handling is Important

Without proper error handling, your script might:

  • Fail silently,

  • Cause data loss,

  • Leave temporary files or broken configurations.

Good error handling makes your scripts robust, debuggable, and production-ready.


πŸ“ Topics We Will Cover

  • βœ… Understanding Exit Status

  • βœ… Using if Statements for Error Checking

  • βœ… Using trap for Cleanup

  • βœ… Redirecting Errors to Logs

  • βœ… Creating Custom Error Messages


🎯 1. Understanding Exit Status

Every Linux command returns an exit status:

  • 0 β†’ Success

  • Non-zero β†’ Failure

You can access it using $?.

πŸ“Œ Example:

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

🎯 2. Using if Statements for Error Checking

It’s good practice to check the success/failure of each critical command.

πŸ“Œ Example:

bashCopyEdit#!/bin/bash
mkdir /tmp/mydir
if [ $? -ne 0 ]; then
  echo "Directory creation failed."
  exit 1
fi

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

🎯 3. Using trap for Cleanup

trap helps clean up resources (e.g., delete temp files) when your script exits unexpectedly.

πŸ“Œ Example:

bashCopyEdit#!/bin/bash
tempfile=$(mktemp)
trap "echo 'Cleaning up...'; rm -f $tempfile" EXIT

echo "Temporary file created: $tempfile"
echo "Sample data" > $tempfile
cat $tempfile

# Simulating an error
exit 1

βœ… Output: Even when exiting with error, the tempfile is deleted.


🎯 4. Redirecting Errors to a File

Redirect error outputs to avoid cluttering terminal logs or to store logs for debugging.

πŸ“Œ Example:

bashCopyEdit#!/bin/bash
cat non_existent_file.txt 2> error.log
echo "Error (if any) logged in error.log"

βœ… 2> redirects stderr (error messages) to a file.


🎯 5. Creating Custom Error Messages

Explain clearly what went wrong so users don’t get confused by cryptic errors.

πŸ“Œ Example:

bashCopyEdit#!/bin/bash
mkdir /tmp/mydir
if [ $? -ne 0 ]; then
  echo "❗ Error: Failed to create /tmp/mydir. Check if you have permission or if the directory exists."
  exit 1
fi

βœ… Summary Tasks

TaskWhat You Practice
Task 1Checking Exit Status (mkdir)
Task 2Using if to handle multi-step errors
Task 3Using trap to handle unexpected exits
Task 4Redirecting errors to error.log
Task 5Writing custom error messages

.


πŸ“ Basic Git Commands You Must Know

1. git init

Initialize a new Git repository.

bashCopyEditgit init

2. git clone

Clone a remote repository from GitHub to your local system.

bashCopyEditgit clone https://github.com/username/repository.git

3. git status

Check the current status of your files.

bashCopyEditgit status

4. git add

Stage files to be committed.

bashCopyEditgit add filename.txt
# or add everything
git add .

5. git commit

Save your changes with a message.

bashCopyEditgit commit -m "Meaningful commit message"

6. git push

Push your commits to the remote GitHub repository.

bashCopyEditgit push origin main

7. git pull

Fetch and merge changes from the remote repository.

bashCopyEditgit pull origin main

8. git branch

List, create, or switch between branches.

bashCopyEditgit branch        # list branches
git checkout -b feature-branch  # create and switch

πŸ’‘ Example Workflow (Typical GitHub Usage)

bashCopyEditgit clone <repo-url>
cd project-directory
git checkout -b new-feature
# make changes
git add .
git commit -m "Added new feature"
git push origin new-feature

Then, you go to GitHub and create a Pull Request to merge your changes!

Resources:https://www.youtube.com/watch?v=q8EevlEpQ2A (Chai aur code Youtube channel)

0
Subscribe to my newsletter

Read articles from Aditya Tiwary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Tiwary
Aditya Tiwary