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


.
π£ 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
Task | What You Practice |
Task 1 | Checking Exit Status (mkdir) |
Task 2 | Using if to handle multi-step errors |
Task 3 | Using trap to handle unexpected exits |
Task 4 | Redirecting errors to error.log |
Task 5 | Writing 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)
Subscribe to my newsletter
Read articles from Aditya Tiwary directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
