<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Muhammad Hanzala Ali</title>
    <description>The latest articles on Forem by Muhammad Hanzala Ali (@hanzalawebdev).</description>
    <link>https://forem.com/hanzalawebdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1667127%2Ff7a85950-3d23-4254-9371-6414ac09102f.jpg</url>
      <title>Forem: Muhammad Hanzala Ali</title>
      <link>https://forem.com/hanzalawebdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hanzalawebdev"/>
    <language>en</language>
    <item>
      <title>BashBlaze Day 4: Building a System Monitoring Script with Bash</title>
      <dc:creator>Muhammad Hanzala Ali</dc:creator>
      <pubDate>Fri, 15 Nov 2024 17:11:16 +0000</pubDate>
      <link>https://forem.com/hanzalawebdev/bashblaze-day-4-building-a-system-monitoring-script-with-bash-1gea</link>
      <guid>https://forem.com/hanzalawebdev/bashblaze-day-4-building-a-system-monitoring-script-with-bash-1gea</guid>
      <description>&lt;p&gt;&lt;strong&gt;Welcome to Day 4 of the BashBlaze&lt;/strong&gt; - 7 Days of Bash Scripting Challenge! Today, we’re tackling an essential skill in the DevOps toolkit: creating a script to monitor system metrics. This task will help you build a deeper understanding of Bash scripting, system monitoring, and user interaction. Ready to take your scripting game up a notch? Let’s dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Goal: A Robust Monitoring Script&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The mission for today’s challenge is to develop a Bash script that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Monitors CPU usage, memory usage, and disk space.&lt;/li&gt;
&lt;li&gt;Provides a simple, interactive menu.&lt;/li&gt;
&lt;li&gt;Continuously displays metrics with user-defined intervals.&lt;/li&gt;
&lt;li&gt;Monitors the status of specific services like Nginx.&lt;/li&gt;
&lt;li&gt;Handles errors gracefully.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Task Breakdown&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Task 1: Basic Metrics Monitoring&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Start by writing a basic script to display CPU, memory, and disk space information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

# Function to display system metrics
show_metrics() {
    echo "\n---- System Metrics ----"
    echo "CPU Usage: $(top -bn1 | grep 'Cpu(s)' | awk '{print $2 + $4}')%"
    echo "Memory Usage: $(free -m | awk '/Mem:/ {printf("%d/%dMB (%.2f%%)", $3, $2, $3*100/$2)}')"
    echo "Disk Space: $(df -h / | awk 'NR==2 {print $5}')"
    echo "\nPress Enter to continue..."
    read
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task 2: User-Friendly Interface&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Enhance the script by creating a menu that allows users to select different options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while true; do
    clear
    echo "---- Monitoring Metrics Script ----"
    echo "1. View System Metrics"
    echo "2. Monitor a Specific Service"
    echo "3. Exit"
    read -p "Choose an option (1-3): " choice

    case $choice in
        1) show_metrics;;
        2) monitor_service;;
        3) echo "Exiting..."; break;;
        *) echo "Invalid option. Please try again."; sleep 2;;
    esac
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task 3: Continuous Monitoring with Sleep&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To enable continuous monitoring, add a loop with a sleep mechanism that pauses between each refresh:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;read -p "Enter sleep interval in seconds: " sleep_interval
while true; do
    show_metrics
    sleep $sleep_interval
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task 4: Monitor Specific Services&lt;/strong&gt;&lt;br&gt;
Expand the script to check the status of services such as Nginx:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;monitor_service() {
    read -p "Enter the service name to monitor: " service
    if systemctl is-active --quiet $service; then
        echo "$service is running."
    else
        echo "$service is not running. Do you want to start it? (Y/N)"
        read start_choice
        if [[ $start_choice == "Y" || $start_choice == "y" ]]; then
            sudo systemctl start $service
            echo "$service started."
        else
            echo "$service was not started."
        fi
    fi
    echo "\nPress Enter to continue..."
    read
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Task 5: Allow User to Choose Different Services&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add flexibility by letting users input any service they wish to monitor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task 6: Error Handling&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ensure the script includes error handling to manage command failures and invalid inputs. Use conditional checks and meaningful error messages to enhance user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This Day 4 challenge empowers you with the knowledge to create a monitoring script that’s both practical and versatile. With user-friendly features and a touch of DevOps flair, this project is an excellent addition to your Bash scripting portfolio.&lt;/p&gt;

&lt;p&gt;What creative name did you give your script? Share your progress and feedback in the comments!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>bash</category>
      <category>linux</category>
      <category>monitoring</category>
    </item>
    <item>
      <title>BashBlaze Day 3: Tackling User Management with Bash (And a Few Laughs!)</title>
      <dc:creator>Muhammad Hanzala Ali</dc:creator>
      <pubDate>Mon, 11 Nov 2024 17:17:23 +0000</pubDate>
      <link>https://forem.com/hanzalawebdev/bashblaze-day-3-tackling-user-management-with-bash-and-a-few-laughs-556a</link>
      <guid>https://forem.com/hanzalawebdev/bashblaze-day-3-tackling-user-management-with-bash-and-a-few-laughs-556a</guid>
      <description>&lt;p&gt;&lt;strong&gt;Day 3 of my Bash Blaze challenge&lt;/strong&gt; has arrived, and it’s packed with user management commands. It was all fun and games until I realized I was the Bash overlord controlling accounts on my system. Here’s how I conquered this challenge and wrote a script so slick that even my cat looked impressed (or maybe it was just yawning).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Challenge Overview: User Management Bash Script&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The task: create a Bash script that handles user accounts like a pro—or at least like a DevOps engineer trying to impress HR.😀&lt;/p&gt;

&lt;h2&gt;
  
  
  The Game Plan:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Account Creation:&lt;/strong&gt; Add users and make sure they’re not already on the list (because cloning is creepy).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Account Deletion:&lt;/strong&gt; Remove users (with their home directories for bonus cleanup points).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Password Reset:&lt;/strong&gt; Reset passwords while pretending I’m a hacker in a movie montage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User Listing:&lt;/strong&gt; Display all users like it’s a roll call at school.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Help Command:&lt;/strong&gt; Because every good script should offer unsolicited advice.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Code That Makes the Magic Happen&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

&amp;lt;&amp;lt;note
Usage: ./user_management.sh [Options]
note

# Displaying usage information like a helpful Bash superhero
function display_usage {
        echo "Usage: ./user_management.sh [Options]"
        echo "Options:"
        echo "-c, --create      Create a new user account."
        echo "-d, --delete      Delete an existing user account."
        echo "-r, --reset       Reset password for an existing user account."
        echo "-l, --list        List all user accounts on the system."
        echo "-h, --help        Display this help and exit."
}

# Creating a new user account (because why not play God?)
function create_user {
        read -p "Enter the new username: " new_user

        if id "$new_user" &amp;amp;&amp;gt;/dev/null; then
                echo "Error: The username '$new_user' already exists. Maybe try 'user2.0'?"
                exit 1
        else
                read -sp "Enter password for $new_user: " password
                sudo useradd -m -p "$(openssl passwd -1 "$password")" "$new_user"
                echo -e "\nUser '$new_user' created successfully. They owe you big time."
        fi
}

# Deleting a user account (peace out, user!)
function delete_user {
        read -p "Enter the username to delete: " username

        if id "$username" &amp;amp;&amp;gt;/dev/null; then
                sudo userdel -r "$username"
                echo "User '$username' deleted. Gone, but not forgotten (unless you use rm -rf)."
        else
                echo "Error: The username '$username' does not exist. Did they ghost us?"
        fi
}

# Resetting passwords (because 'password123' is so last season)
function reset_passwd {
        read -p "Enter the username to reset password for: " reset_user

        if id "$reset_user" &amp;amp;&amp;gt;/dev/null; then
                read -sp "Enter new password for '$reset_user': " password
                echo "$reset_user:$password" | sudo chpasswd
                echo -e "\nPassword for '$reset_user' reset. Use wisely, young Padawan."
        else
                echo "Error: '$reset_user' not found. Check your spelling or your luck."
        fi
}

# Listing user accounts (because we're nosy)
function list_users {
        echo "Here are the current VIPs (Very Important Password-holders):"
        awk -F: '{ print "- " $1 " (UID: " $3 ")" }' /etc/passwd
}

# Script begins here
if [ $# -eq 0 ] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
        display_usage
        exit 0
fi

while [ $# -gt 0 ]; do
        case "$1" in
                -c | --create)
                        create_user
                        ;;
                -d | --delete)
                        delete_user
                        ;;
                -r | --reset)
                        reset_passwd
                        ;;
                -l | --list)
                        list_users
                        ;;
                *)
                        echo "Error: Invalid option '$1'. Try '--help' for better luck."
                        exit 1
                        ;;
        esac
        shift
done

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Key Bash Commands for User Management&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Creating Users&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;sudo useradd -m username&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The -m flag ensures a home directory is created for the new user. This simple command is a must for adding users quickly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Setting Passwords&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;sudo passwd username&lt;/code&gt;&lt;br&gt;
This command lets you set or change a password for a user, essential for maintaining secure access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Deleting Users&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;sudo userdel -r username&lt;/code&gt;&lt;br&gt;
The &lt;code&gt;-r&lt;/code&gt; flag removes the user's home directory along with their profile, ensuring a complete cleanup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Adding Users to Groups&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;sudo usermod -aG groupname username&lt;/code&gt;&lt;br&gt;
The &lt;code&gt;-aG&lt;/code&gt; option appends the user to an existing group without removing them from other groups.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Pro Tip:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Always double-check user permissions with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;groups username&lt;/code&gt;&lt;br&gt;
This helps you verify that the user is part of the correct groups and has the appropriate access.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Challenges Faced and How I Overcame Them&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;During Day 3, I encountered scenarios where user permissions needed to be fine-tuned for specific scripts. Here’s how I tackled these challenges:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Error: User already exists&lt;br&gt;
Solution: Use id username to check user existence before creation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Permissions Denied&lt;br&gt;
Solution: Apply chmod and chown commands to fix permissions on directories.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Real-World Application&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;User management is not just theoretical; it plays a significant role in CI/CD pipelines, ensuring only authorized users can deploy code or make changes. Integrating these Bash skills into your daily tasks can simplify user access management and enhance security protocols.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Day 3 of my Bash Blaze challenge was both insightful and practical. Learning these user management commands has empowered me to be more confident in my DevOps journey. As I move on to Day 4, which promises even more exciting concepts, I invite you to follow along and join me in mastering Bash scripting.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>devops</category>
      <category>bashblaze</category>
      <category>linux</category>
    </item>
    <item>
      <title>BashBlaze Day 2: Directory Backup with Rotation (Part-2)</title>
      <dc:creator>Muhammad Hanzala Ali</dc:creator>
      <pubDate>Thu, 31 Oct 2024 17:40:33 +0000</pubDate>
      <link>https://forem.com/hanzalawebdev/bashblaze-day-2-directory-backup-with-rotation-part-2-1he0</link>
      <guid>https://forem.com/hanzalawebdev/bashblaze-day-2-directory-backup-with-rotation-part-2-1he0</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Welcome back to Day 2 of the BashBlaze - 7 Days of Bash Scripting Challenge!&lt;/strong&gt; &lt;br&gt;
For this challenge, we’ll create a bash script to automate directory backups with rotation, ensuring that only the most recent versions are stored. This is especially useful in real-world scenarios where storage space matters, and we need to manage backup files efficiently.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Challenge Description&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Our goal is to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a bash script that accepts a directory path as a command-line argument.&lt;/li&gt;
&lt;li&gt;Generate timestamped backups for the specified directory.&lt;/li&gt;
&lt;li&gt;Implement a rotation mechanism to keep only the last three backups, removing the oldest ones automatically.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The script will backup all files in the specified directory and ensure we retain only the three latest backups by removing older ones, keeping our backup folder clutter-free and manageable.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Solution Code&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here’s the bash script that accomplishes our goal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

&amp;lt;&amp;lt;note
This script creates a timestamped backup with rotation, keeping only the last 3 backups.

Usage:
./backup_with_rotation.sh &amp;lt;source_directory&amp;gt; &amp;lt;backup_directory&amp;gt;
note

# Function to display usage information
function display_usage {
    echo "Usage: ./backup_with_rotation.sh &amp;lt;source_directory&amp;gt; &amp;lt;backup_directory&amp;gt;"
}

# Validate command-line arguments
if [ $# -ne 2 ] || [ ! -d "$1" ]; then
    echo "Error: Please provide a valid source directory path and a backup directory."
    display_usage
    exit 1
fi

# Define source and backup directories
source_dir=$1
backup_dir=$2
timestamp=$(date '+%Y-%m-%d-%H-%M-%S')

# Function to create a timestamped backup
function create_backup {
    zip -r "${backup_dir}/backup_${timestamp}.zip" "${source_dir}" &amp;gt; /dev/null
    if [ $? -eq 0 ]; then
        echo "Backup created successfully: ${backup_dir}/backup_${timestamp}.zip"
    fi
}

# Function to keep only the last 3 backups
function perform_rotation {
    # List backups in reverse chronological order
    backups=($(ls -t "${backup_dir}/backup_"*.zip 2&amp;gt;/dev/null))

    # If there are more than 3 backups, delete the oldest ones
    if [ "${#backups[@]}" -gt 3 ]; then
        echo "Performing rotation to keep only the last 3 backups..."
        backups_to_remove=("${backups[@]:3}")
        for backup in "${backups_to_remove[@]}"; do
            rm -f "${backup}"
            echo "Removed old backup: $backup"
        done
    fi
}

# Main script execution
create_backup
perform_rotation

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;How It Works&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here’s a breakdown of the main components of the script:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;## &lt;strong&gt;Usage Information:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The script begins by providing a usage note to guide users on the command format.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;## &lt;strong&gt;Command-Line Argument Validation:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ensures that both source and backup directories are provided.&lt;br&gt;
Displays an error message if the arguments are missing or invalid.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;## &lt;strong&gt;Timestamped Backup Creation:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Uses the &lt;code&gt;create_backup&lt;/code&gt; function to generate a compressed &lt;code&gt;.zip&lt;/code&gt; backup of the source directory.&lt;br&gt;
Names the backup using the current timestamp for easy tracking.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;## &lt;strong&gt;Backup Rotation:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;perform_rotation&lt;/code&gt; function checks if there are more than three backups.&lt;/p&gt;

&lt;p&gt;If so, it removes the oldest backups, keeping only the latest three, thus saving space and maintaining recent copies.&lt;/p&gt;
&lt;h2&gt;
  
  
  Example Usage
&lt;/h2&gt;

&lt;p&gt;Here’s what the script looks like in action:&lt;/p&gt;
&lt;h2&gt;
  
  
  First Execution
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./backup_with_rotation.sh /home/user/documents /home/user/backups
Output:
Backup created successfully: /home/user/backups/backup_2023-07-30_12-30-45.zip

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Second Execution with Rotation
&lt;/h2&gt;

&lt;p&gt;When there are more than three backups, the oldest one is removed automatically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./backup_with_rotation.sh /home/user/documents /home/user/backups
Output:
Backup created successfully: /home/user/backups/backup_2023-08-01_09-15-30.zip
Performing rotation to keep only the last 3 backups...
Removed old backup: /home/user/backups/backup_2023-07-30_12-30-45.zip

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Today’s script covers essential automation for backup management with a focus on storage efficiency. By using rotation to retain only the most recent backups, this solution is practical and scalable, especially for servers and personal systems that require regular backups.&lt;/p&gt;

&lt;h2&gt;
  
  
  Submission Instructions
&lt;/h2&gt;

&lt;p&gt;Push your backup_with_rotation.sh script to your GitHub repository and share your progress with the community! Looking forward to hearing about your experience and any creative ways you apply this script!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>bash</category>
    </item>
    <item>
      <title>BashBlaze Day 2: Building an Interactive File and Directory Explorer (Part-1)</title>
      <dc:creator>Muhammad Hanzala Ali</dc:creator>
      <pubDate>Wed, 30 Oct 2024 18:16:35 +0000</pubDate>
      <link>https://forem.com/hanzalawebdev/bashblaze-day-2-building-an-interactive-file-and-directory-explorer-part-1-17ch</link>
      <guid>https://forem.com/hanzalawebdev/bashblaze-day-2-building-an-interactive-file-and-directory-explorer-part-1-17ch</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;*&lt;em&gt;Welcome to Day 2 of the BashBlaze - 7 Days of Bash Scripting Challenge! *&lt;/em&gt;&lt;br&gt;
Today, we’re stepping up our bash scripting skills by creating an Interactive File and Directory Explorer that not only lists files and directories in the current path but also provides a character counting feature for any text entered by the user.&lt;/p&gt;

&lt;p&gt;This challenge is a hands-on approach to working with loops, command-line interaction, and command chaining in bash. Let’s dive into the tasks and explore how each part of this interactive script works!&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Challenge Breakdown&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Our script has two main functions:&lt;/p&gt;

&lt;p&gt;Part 1: File and Directory Exploration&lt;br&gt;
Part 2: Character Counting&lt;br&gt;
Let’s go over each of these parts in detail with code examples.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Part 1: File and Directory Exploration&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the first part of this challenge, we’re building an interactive explorer that displays all files and directories in the current path in a human-readable format.&lt;/p&gt;

&lt;p&gt;Here's the solution for Part 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

# Part1: Files &amp;amp; Directory Exploration
echo "Welcome to the Interactive File and Directory Explorer!"

while true; do
    # List all files and directories in the current path
    echo "Files and Directories in the Current Path:"
    ls -lh

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ls -lh&lt;/code&gt; command lists all files and directories in the current path. The &lt;code&gt;-l&lt;/code&gt; flag shows detailed information, and &lt;code&gt;-h&lt;/code&gt; makes sizes human-readable (KB, MB, etc.).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Part 2: Character Counting&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;After displaying files and directories, the script moves to character counting for user input. It prompts the user to enter a line of text and counts the characters in each line until the user presses Enter without entering text, which exits the script.&lt;/p&gt;

&lt;p&gt;Here’s the solution for Part 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  # Part2: Character Counting
    read -p "Enter a line of text (Press Enter without text to exit): " input

    # Exit if the input is empty
    if [[ -z "$input" ]]; then
          echo "Exiting the Interactive Explorer. Goodbye!"
          break
    fi

    # Calculate and print the character count for the input line
    char_count=$(echo -n "$input" | wc -m)
    echo "Character count: $char_count"
done

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Explanation:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;read -p&lt;/code&gt; prompts the user for input.&lt;br&gt;
&lt;code&gt;if [[ -z "$input" ]];&lt;/code&gt; then ... checks if the input is empty and breaks the loop if so.&lt;br&gt;
&lt;code&gt;echo -n "$input" | wc -m&lt;/code&gt; counts the characters in the input without counting the newline.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Full Solution&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here’s the complete solution for Day 2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

# Part1: Files &amp;amp; Directory Exploration
echo "Welcome to the Interactive File and Directory Explorer!"

while true; do
    # List all files and directories in the current path
    echo "Files and Directories in the Current Path:"
    ls -lh

    # Part2: Character Counting
    read -p "Enter a line of text (Press Enter without text to exit): " input

    # Exits when the input is empty
    if [[ -z "$input" ]]; then
          echo "Exiting the Interactive Explorer. Goodbye!"
          break
    fi

    # Calculate and print the character count for the input line
    char_count=$(echo -n "$input" | wc -m)
    echo "Character count: $char_count"
done

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Example Interaction:&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ./explorer.sh
Welcome to the Interactive File and Directory Explorer!

Files and Directories in the Current Path:
- file1.txt (100 KB)
- dir1 (2 MB)
- script.sh (3 KB)

Enter a line of text (Press Enter without text to exit): Hello, this is a sample line.
Character Count: 27

Enter a line of text (Press Enter without text to exit): Another line to count.
Character Count: 25

Enter a line of text (Press Enter without text to exit):
Exiting the Interactive Explorer. Goodbye!

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Today’s challenge was all about building an interactive experience using bash scripting. We learned how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use loops to maintain an interactive session.&lt;/li&gt;
&lt;li&gt;Display files and directories with human-readable sizes.&lt;/li&gt;
&lt;li&gt;Capture and count characters in user input.
This exercise has shown the power of bash scripting for real-world tasks. Stay tuned for Day 3, where we’ll explore even more advanced topics in bash scripting.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>bash</category>
      <category>challenge</category>
      <category>devops</category>
      <category>bashblaze</category>
    </item>
    <item>
      <title>BashBlaze Day 1: Mastering the Basics of Bash Scripting</title>
      <dc:creator>Muhammad Hanzala Ali</dc:creator>
      <pubDate>Sun, 20 Oct 2024 13:58:09 +0000</pubDate>
      <link>https://forem.com/hanzalawebdev/bashblaze-day-1-mastering-the-basics-of-bash-scripting-3h6k</link>
      <guid>https://forem.com/hanzalawebdev/bashblaze-day-1-mastering-the-basics-of-bash-scripting-3h6k</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Welcome to Day 1 of the BashBlaze - 7 Days of Bash Scripting Challenge! Whether you're new to scripting or just brushing up, this day is all about the fundamentals of bash scripting. We'll cover comments, echo commands, variables, built-in variables, and wildcards to get you started. Each task is designed to give you hands-on experience with essential bash scripting features.&lt;/p&gt;

&lt;p&gt;Let’s dive in and go through each task with a solution so you can follow along and implement them yourself!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Task 1: Comments in Bash&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Comments are essential for making your scripts readable and maintainable. They help you and others understand what the script is doing at a glance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Task 1: Comments
&amp;lt;&amp;lt; comment
    This script demonstrates how to use comments in bash.
comment

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this script, we used both single-line comments (using #) and multi-line comments (using a here-document &amp;lt;&amp;lt; comment). Adding comments to your code is a great way to provide context and make your scripts easy to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Task 2: Using Echo in Bash&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The echo command is one of the simplest yet most powerful commands for outputting text in bash scripts. You can use it to print any message to the terminal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Task 2: Echo
echo "Hello World!"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This script prints "Hello World!" to the terminal, a classic first step in learning any programming language. The echo command can be used in combination with variables and complex expressions, which we will explore in later tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Task 3: Declaring Variables&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Variables allow us to store and reuse data throughout our scripts. Let’s declare a variable and print its value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Task 3: Variables
text="Declare Variable"
echo "$text"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we store the string "Declare Variable" in the variable text and print its value using echo. Remember to use $ when referencing the value of a variable in bash.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Task 4: Using Variables to Perform Arithmetic&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Variables are not just for strings; you can also use them for numerical operations. Let's use variables to calculate the sum of two numbers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Task 4: Using Variables
a=5
b=3
sum=$((a+b))
echo "The sum is $sum"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we declare two variables a and b with the values 5 and 3, respectively. We then use the $(( ... )) syntax to perform arithmetic and print the result.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Task 5: Working with Built-in Variables&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Bash comes with several built-in variables that provide useful information about the environment. Let's explore some of them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Task 5: Using Built-in Variables
echo "Exit status of the last command: $?"
echo "Process ID of the current script: $$"
echo "Current working directory: $PWD"
echo "Home directory: $HOME"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;$? returns the exit status of the last executed command.&lt;/li&gt;
&lt;li&gt;$$ gives the process ID of the current script.&lt;/li&gt;
&lt;li&gt;$PWD displays the current working directory.&lt;/li&gt;
&lt;li&gt;$HOME shows the home directory path.
These built-in variables are very useful in real-world scripting scenarios.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Task 6: Using Wildcards in Bash&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Wildcards are used for pattern matching, making it easy to work with files and directories. Let’s use a wildcard to list all files with a specific extension.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Task 6: Wildcards
echo "List all .sh files in the current directory:"
ls *.sh

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The *.sh wildcard matches all files with the .sh extension in the current directory. Wildcards are extremely handy when working with multiple files, especially for tasks like file manipulation and searching.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Congratulations on completing Day 1 of the BashBlaze Challenge! Today we covered the basics of bash scripting, from comments and echo commands to variables, built-in variables, and wildcards. These foundational skills will set you up for success as we move on to more advanced topics in the coming days. Stay tuned for Day 2, where we'll dive deeper into conditional statements and loops!&lt;/p&gt;

&lt;p&gt;Make sure to commit your script to GitHub and share your progress. Happy scripting!&lt;/p&gt;

&lt;p&gt;My GitHub repository link: &lt;a href="https://github.com/hanzalawebdev/BashBlaze-7-Days-of-Bash-Scripting-Challenge/tree/main" rel="noopener noreferrer"&gt;&lt;u&gt;Click Here&lt;/u&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Key Takeaways&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;*&lt;em&gt;Comments *&lt;/em&gt; help in explaining your code.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;echo&lt;/strong&gt; command is used to print messages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variables&lt;/strong&gt; store data that can be reused.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Built-in Variables&lt;/strong&gt; provide useful information like the process ID, current directory, and more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wildcards&lt;/strong&gt; allow you to match file patterns efficiently.
If you found this helpful, be sure to check back for Day 2 of the BashBlaze Challenge, where we’ll take things up a notch!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>bash</category>
      <category>linux</category>
      <category>devops</category>
      <category>challenge</category>
    </item>
  </channel>
</rss>
