Practical Scripts

Practical Scripts

Concepts

This lesson brings together everything from the shell scripting module. No new concepts — just real-world application. Each script below is a practical tool you might actually use. Study them, run them, and modify them.


Lab

Script 1: Backup Script

A script that creates timestamped compressed backups of a directory.

cd ~/lab/scripts

cat > backup.sh << 'SCRIPT'
#!/bin/bash
set -euo pipefail

# --- Configuration ---
BACKUP_DIR="${HOME}/backups"

# --- Functions ---
die() { echo "ERROR: $1" >&2; exit 1; }
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"; }

usage() {
    echo "Usage: $0 <source_directory>"
    echo "Creates a timestamped backup in $BACKUP_DIR"
    exit 1
}

# --- Validate ---
[[ $# -eq 1 ]] || usage

SOURCE="$1"
[[ -d "$SOURCE" ]] || die "Source directory does not exist: $SOURCE"

# --- Prepare ---
mkdir -p "$BACKUP_DIR"

BASENAME=$(basename "$SOURCE")
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="${BACKUP_DIR}/${BASENAME}_${TIMESTAMP}.tar.gz"

# --- Execute ---
log "Starting backup of '$SOURCE'"
log "Destination: $BACKUP_FILE"

tar -czf "$BACKUP_FILE" -C "$(dirname "$SOURCE")" "$BASENAME"

SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
log "Backup complete: $BACKUP_FILE ($SIZE)"

# --- Show recent backups ---
echo ""
echo "Recent backups:"
ls -lht "$BACKUP_DIR"/${BASENAME}_*.tar.gz 2>/dev/null | head -5
SCRIPT

chmod +x backup.sh

# Test it
mkdir -p ~/lab/testdata
echo "important file" > ~/lab/testdata/data.txt
echo "config" > ~/lab/testdata/settings.conf

./backup.sh ~/lab/testdata
ls -lh ~/backups/

Script 2: Log Analyzer

A script that parses a log file and reports statistics.

[Read more]

Processes and Job Control

Processes and Job Control

Concepts

What Is a Process?

A process is a running instance of a program. When you type ls, the shell creates a process to execute the ls program. When the command finishes, the process dies.

Every process has:

Attribute Description
PID Process ID — a unique number identifying the process
PPID Parent Process ID — the PID of the process that started it
UID User ID — which user owns the process
State Running, sleeping, stopped, zombie, etc.
Memory How much RAM the process is using
CPU How much CPU time it is consuming

When you open a terminal, a shell process (e.g., Bash, PID 1234) starts. Every command you run becomes a child process of that shell. The system’s first process (PID 1) is systemd (or init), which is the ancestor of all processes.

[Read more]

Repositories, PPAs, and Backports

Repositories, PPAs, and Backports

Concepts

What Is a Repository?

A repository (or “repo”) is a server that hosts packages. When you run apt update, APT downloads the package index from your configured repositories. When you run apt install, it downloads the package from one of those repositories.

Your system knows which repositories to use from the configuration in:

Ubuntu 24.04: /etc/apt/sources.list.d/ubuntu.sources (new DEB822 format)

Debian 12: /etc/apt/sources.list (traditional one-line format)

[Read more]

Scheduling with cron and at

Scheduling with cron and at

Concepts

cron — Recurring Scheduled Tasks

cron runs commands on a schedule — every minute, every day, every Monday, etc. The cron daemon (cron or crond) checks schedules once per minute.

Crontab Syntax

Each line in a crontab defines one scheduled task:

┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12 or jan-dec)
│ │ │ │ ┌─ day of week (0-7, 0 and 7 = Sunday, or sun-sat)
│ │ │ │ │
* * * * *  command to execute

Examples

# Every minute
* * * * *  /usr/local/bin/check-health.sh

# Every day at 3:30 AM
30 3 * * *  /home/kmiguel/backup.sh

# Every Monday at 8 AM
0 8 * * 1  echo "Week started" | mail -s "Monday" kmiguel

# Every 15 minutes
*/15 * * * *  /usr/local/bin/sync.sh

# First day of every month at midnight
0 0 1 * *  /home/kmiguel/monthly-report.sh

# Weekdays at 6 PM
0 18 * * 1-5  /home/kmiguel/eod-report.sh

# Every 6 hours
0 */6 * * *  /usr/local/bin/cleanup.sh

Special Strings

@reboot    command     # run once at startup
@hourly    command     # 0 * * * *
@daily     command     # 0 0 * * *
@weekly    command     # 0 0 * * 0
@monthly   command     # 0 0 1 * *
@yearly    command     # 0 0 1 1 *

Managing Your Crontab

# Edit your crontab
crontab -e

# List your crontab
crontab -l

# Remove your entire crontab (careful!)
crontab -r

# Edit another user's crontab (root only)
sudo crontab -u username -e

Cron Tips

Output handling: By default, cron emails any output to the user. Since most systems don’t have a local mail setup, redirect output:

[Read more]

sed and awk

sed and awk

Concepts

sed — Stream Editor

sed processes text line by line, applying transformations. It reads input, applies rules, and writes the result to stdout. The original file is unchanged unless you use the in-place flag (-i).

The most common use of sed is search and replace.

sed Substitution

The core syntax:

sed 's/pattern/replacement/' file
#    │ │       │            │
#    │ │       │            └─ input
#    │ │       └─ what to replace with
#    │ └─ what to find (regex)
#    └─ s = substitute command

By default, s replaces only the first occurrence on each line. Add g for all occurrences:

[Read more]