Bash Scripting Projects: From Beginner to Advanced

Bash Scripting Projects: From Beginner to Advanced

Bash scripting is a powerful way to automate tasks and manage systems efficiently. Below are bash scripts for various projects, each with basic functionality and comments for better understanding. Whether you're a beginner or looking for more advanced projects, this guide has something for everyone.

Beginner Projects

1. Simple Calculator

This script performs basic arithmetic operations.

#!/bin/bash

echo "Simple Calculator"
echo "Enter first number: "
read a
echo "Enter second number: "
read b

echo "Choose operation: +, -, *, /"
read op

case $op in
    +) result=$(echo "$a + $b" | bc) ;;
    -) result=$(echo "$a - $b" | bc) ;;
    \*) result=$(echo "$a * $b" | bc) ;;
    /) result=$(echo "$a / $b" | bc) ;;
    *) echo "Invalid operation"; exit 1 ;;
esac

echo "Result: $result"

2. File Organizer

This script organizes files by their extension into corresponding directories.

#!/bin/bash

echo "Organizing files by extension..."

for file in *.*; do
    ext="${file##*.}"
    mkdir -p "$ext"
    mv "$file" "$ext/"
done

echo "Files organized."

3. System Information Display

This script displays basic system information.

#!/bin/bash

echo "System Information"
echo "------------------"
echo "Hostname: $(hostname)"
echo "Uptime: $(uptime -p)"
echo "Memory Usage: "
free -h
echo "Disk Usage: "
df -h
echo "Logged in users: "
who

4. Backup Script

This script creates a backup of the specified directory.

#!/bin/bash

echo "Starting backup..."
src="$HOME/Documents"
dest="$HOME/backup_$(date +%Y%m%d%H%M%S)"
mkdir -p "$dest"
cp -r "$src" "$dest"

echo "Backup completed at $dest"

5. Random Password Generator

This script generates a random password.

#!/bin/bash

length=12
echo "Generating a random password..."
password=$(openssl rand -base64 48 | cut -c1-$length)
echo "Your random password is: $password"

Intermediate Projects

1. Log File Analyzer

This script analyzes log files and shows the top 5 most common log entries.

#!/bin/bash

log_file="/var/log/syslog"
echo "Analyzing log file: $log_file"

echo "Top 5 most common log entries:"
grep -oE '^[^ ]+' "$log_file" | sort | uniq -c | sort -nr | head -n 5

2. Web Scraper

This script scrapes the title from a web page.

#!/bin/bash

url="http://example.com"
echo "Scraping $url"
curl -s "$url" | grep -o '<title>[^<]*' | sed 's/<title>//'

echo "Scraping completed."

3. Automated Database Backup

This script creates a backup of a MySQL database.

#!/bin/bash

db_user="root"
db_password="password"
db_name="mydatabase"
backup_dir="$HOME/db_backups"

mkdir -p "$backup_dir"
backup_file="$backup_dir/$db_name-$(date +%Y%m%d%H%M%S).sql"

echo "Backing up database $db_name..."
mysqldump -u "$db_user" -p"$db_password" "$db_name" > "$backup_file"
echo "Backup completed: $backup_file"

4. System Monitoring Dashboard

This script provides a continuous system monitoring dashboard.

#!/bin/bash

echo "System Monitoring Dashboard"
while true; do
    echo "-----------------------"
    echo "Date: $(date)"
    echo "Uptime: $(uptime -p)"
    echo "Memory Usage: "
    free -h
    echo "Disk Usage: "
    df -h | grep '^/dev/'
    echo "CPU Load: "
    top -bn1 | grep "Cpu(s)"
    sleep 5
done

5. Automated Software Installation Script

This script installs a list of software packages.

#!/bin/bash

echo "Installing software packages..."
packages=(git vim curl wget)

for package in "${packages[@]}"; do
    if ! dpkg -l | grep -qw "$package"; then
        sudo apt-get install -y "$package"
    else
        echo "$package is already installed."
    fi
done

echo "Software installation completed."

Advanced Projects

1. Network Port Scanner

This script scans open ports on a given IP address.

#!/bin/bash

echo "Enter the IP address to scan: "
read ip
echo "Scanning ports on $ip..."
for port in {1..65535}; do
    timeout 1 bash -c "echo '' > /dev/tcp/$ip/$port" 2>/dev/null && echo "Port $port is open"
done
echo "Scan completed."

2. Distributed Backup System

This script performs a distributed backup to multiple servers.

#!/bin/bash

src="$HOME/Documents"
dest_servers=("server1:/backup" "server2:/backup")

for server in "${dest_servers[@]}"; do
    echo "Backing up to $server..."
    rsync -avz "$src" "$server"
done

echo "Distributed backup completed."

3. Custom Shell with Advanced Features

This script creates a custom shell with basic command execution.

#!/bin/bash

echo "Welcome to My Custom Shell!"
while true; do
    echo -n "my-shell> "
    read cmd
    case "$cmd" in
        exit) break ;;
        *) eval "$cmd" ;;
    esac
done
echo "Goodbye!"

4. Automated Server Provisioning Tool

This script automates server provisioning.

#!/bin/bash

server_list=("server1" "server2")
echo "Provisioning servers..."

for server in "${server_list[@]}"; do
    echo "Provisioning $server..."
    ssh root@"$server" <<EOF
    apt-get update
    apt-get upgrade -y
    apt-get install -y nginx
    systemctl start nginx
    systemctl enable nginx
EOF
done

echo "Server provisioning completed."

5. Intrusion Detection System

This script monitors the system log for intrusion attempts.

#!/bin/bash

log_file="/var/log/auth.log"
echo "Monitoring $log_file for intrusion attempts..."

tail -F "$log_file" | while read line; do
    if echo "$line" | grep -q "Failed password"; then
        echo "Intrusion attempt detected: $line"
    fi
done

These scripts provide a foundation for each project and can be expanded with additional functionality as needed.


Feel free to use and expand on these scripts for your projects. Happy scripting!