Wednesday, April 30, 2025

Advanced Bash Scripting Examples for Ubuntu

 

Advanced Bash Scripting Examples for Ubuntu

Here are some powerful Bash scripting examples that will take your Ubuntu terminal skills to the next level:

1. System Monitoring Script

bash
Copy
Download
#!/bin/bash

# System Monitor
echo "===== SYSTEM MONITOR ====="
echo "Date: $(date)"
echo "Uptime: $(uptime -p)"
echo "CPU Load: $(uptime | awk -F'load average: ' '{print $2}')"
echo "Memory Usage: $(free -h | grep Mem | awk '{print $3"/"$2}')"
echo "Disk Usage: $(df -h / | tail -1 | awk '{print $5}')"

Usage:

  1. Save as monitor.sh

  2. chmod +x monitor.sh

  3. Run with ./monitor.sh

2. Automated Backup Script

bash
Copy
Download
#!/bin/bash

# Auto Backup
BACKUP_DIR="/backups"
SOURCE_DIR="$HOME/Documents"
DATE=$(date +%Y-%m-%d)

mkdir -p $BACKUP_DIR
tar -czf "$BACKUP_DIR/backup_$DATE.tar.gz" "$SOURCE_DIR"
echo "Backup created: $BACKUP_DIR/backup_$DATE.tar.gz"

Usage:

  1. Save as backup.sh

  2. Make executable (chmod +x backup.sh)

  3. Run daily with cron

3. Network Scanner

bash
Copy
Download
#!/bin/bash

# Network Scanner
echo "Scanning local network..."
for ip in 192.168.1.{1..254}; do
    ping -c 1 -W 1 $ip &>/dev/null && echo "$ip is UP"
done

Usage:

  1. Save as netscan.sh

  2. chmod +x netscan.sh

  3. Run with sudo ./netscan.sh

4. File Organizer

bash
Copy
Download
#!/bin/bash

# File Organizer
for file in *; do
    if [[ -f "$file" ]]; then
        ext="${file##*.}"
        mkdir -p "$ext"
        mv "$file" "$ext/"
    fi
done

Usage:

  1. Save as organizer.sh

  2. chmod +x organizer.sh

  3. Run in any directory to sort files by extension

5. Password Generator

bash
Copy
Download
#!/bin/bash

# Password Generator
length=12
echo "Generating $length-character password:"
tr -dc 'A-Za-z0-9!@#$%^&*()' < /dev/urandom | head -c $length
echo

Usage:

  1. Save as passgen.sh

  2. chmod +x passgen.sh

  3. Run with ./passgen.sh

6. Log File Analyzer

bash
Copy
Download
#!/bin/bash

# Log Analyzer
logfile="/var/log/syslog"
echo "Top 10 IPs in $logfile:"
grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $logfile | sort | uniq -c | sort -nr | head

Usage:

  1. Save as loganalyzer.sh

  2. chmod +x loganalyzer.sh

  3. Run with sudo ./loganalyzer.sh

7. Automated Updates

bash
Copy
Download
#!/bin/bash

# System Updater
echo "Starting system update..."
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -y
echo "Update complete!"

Usage:

  1. Save as update.sh

  2. chmod +x update.sh

  3. Run weekly with cron

8. Weather Checker

bash
Copy
Download
#!/bin/bash

# Weather Check
city=${1:-London}
curl -s "wttr.in/$city?0"

Usage:

  1. Save as weather.sh

  2. chmod +x weather.sh

  3. Run with ./weather.sh Paris

9. SSH Connection Manager

bash
Copy
Download
#!/bin/bash

# SSH Manager
echo "Available servers:"
echo "1) Web Server (user@192.168.1.10)"
echo "2) Database (user@192.168.1.20)"
read -p "Choose server: " choice

case $choice in
    1) ssh user@192.168.1.10 ;;
    2) ssh user@192.168.1.20 ;;
    *) echo "Invalid choice" ;;
esac

Usage:

  1. Save as ssh-manager.sh

  2. chmod +x ssh-manager.sh

  3. Run with ./ssh-manager.sh

10. Disk Space Alert

bash
Copy
Download
#!/bin/bash

# Disk Space Monitor
THRESHOLD=90
CURRENT=$(df / | grep / | awk '{print $5}' | sed 's/%//g')

if [ "$CURRENT" -gt "$THRESHOLD" ]; then
    echo "Warning: Disk space is at ${CURRENT}%!" | mail -s "Disk Alert" admin@example.com
fi

Usage:

  1. Save as disk-alert.sh

  2. chmod +x disk-alert.sh

  3. Add to cron for daily checks

Bonus: Make Scripts Executable Everywhere

bash
Copy
Download
# Add to ~/.bashrc
export PATH=$PATH:~/scripts

Then store all scripts in ~/scripts and run them from anywhere!


Next Steps

  • Learn about cron for scheduling (crontab -e)

  • Explore awk and sed for text processing

  • Study grep with regular expressions

  • Try making interactive scripts with read


No comments:

Post a Comment