Przejdลบ do treล›ci

๐Ÿ“˜ AI Shell: Examples

Real-world examples demonstrating the power of AI-augmented shell scripting when done safely and responsibly.


๐ŸŽฏ Example 1: Dynamic Log Analyzer

Prompt:

Write a POSIX shell script that takes a log file path as input and: 1. Counts error lines. 2. Shows top 5 IP addresses involved in errors. 3. Outputs a JSON summary.

AI-Generated Script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/bin/sh
set -eu

logfile="$1"

if [ ! -f "$logfile" ]; then
    echo '{"error": "File not found"}'
    exit 1
fi

error_count=$(grep -c "ERROR" "$logfile")
top_ips=$(grep "ERROR" "$logfile" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sort | uniq -c | sort -nr | head -5)

# Build JSON manually (safe for small payloads)
printf '{\n  "error_count": %d,\n  "top_ips": [\n' "$error_count"
echo "$top_ips" | while read -r count ip; do
    printf '    {"ip": "%s", "count": %d},\n' "$ip" "$count"
done | sed '$ s/,$//'
printf '  ]\n}\n'

Safety Review:

โœ… POSIX-compliant โœ… Handles missing file โœ… No hardcoded paths โœ… Safe grep/uniq usage


๐ŸŽฏ Example 2: Automated Backup Script with Rotation

Prompt:

Write a POSIX-compliant backup script that: - Takes a directory to backup and a destination. - Compresses it with tar. - Keeps only the last 7 backups. - Logs to stdout in JSON format.

AI-Generated Script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/sh
set -eu

src_dir="$1"
dest_dir="$2"

if [ ! -d "$src_dir" ]; then
    echo '{"status": "error", "message": "Source directory does not exist"}'
    exit 1
fi

mkdir -p "$dest_dir"

timestamp=$(date -u +"%Y%m%d_%H%M%S")
backup_name="backup_$timestamp.tar.gz"
backup_path="$dest_dir/$backup_name"

# Create backup
if tar -czf "$backup_path" -C "$(dirname "$src_dir")" "$(basename "$src_dir")"; then
    echo "{\"status\": \"success\", \"backup\": \"$backup_path\"}"
else
    echo "{\"status\": \"error\", \"message\": \"Backup failed\"}"
    exit 1
fi

# Rotate backups
ls -t "$dest_dir"/backup_*.tar.gz 2>/dev/null | tail -n +8 | xargs -r rm --

Safety Review:

โœ… POSIX date usage โœ… Safe tar with -C โœ… Backup rotation โœ… JSON logging


๐ŸŽฏ Example 3: AI-Assisted Docker Healthcheck

Prompt:

Write a healthcheck script for a Docker container running a web service. It should: 1. Check if the process is running. 2. Check if port 8080 is open. 3. Return 0 if healthy, 1 otherwise.

AI-Generated Script:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/bin/sh
set -e

# Check if main process is running
if ! pgrep -f "webserver" >/dev/null; then
    echo "Web server process not found"
    exit 1
fi

# Check if port is open
if ! nc -z localhost 8080 2>/dev/null; then
    echo "Port 8080 is not open"
    exit 1
fi

echo "Healthcheck passed"
exit 0

Safety Review:

โœ… Uses pgrep (widely available) โœ… Uses nc (common in containers) โœ… Clear error messages โœ… Safe exit codes


๐Ÿงช Example 4: AI-Guided System Update (via Tool Calling)

AI Conversation Flow:

  1. AI Prompt: "Check if system updates are available." โ†’ Tool Call: {"cmd": "apt list --upgradable 2>/dev/null | grep -v Listing"}

  2. Executor Returns:

    1
    curl/stable 7.81.0-1 amd64 [upgradable from: 7.74.0-1]
    

  3. AI Prompt: "Install the updates safely." โ†’ Tool Call: {"cmd": "apt-get update && apt-get upgrade -y"}

  4. Executor Runs Command

  5. AI Reports: "System updated successfully."


๐Ÿงพ Summary

These examples show: โœ… How to prompt for safe, structured output โœ… How to integrate with tool calling โœ… How to handle errors gracefully โœ… How to make scripts portable and idempotent


๐Ÿงพ See Also