๐ 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 | |
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 | |
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 | |
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:
-
AI Prompt: "Check if system updates are available." โ Tool Call:
{"cmd": "apt list --upgradable 2>/dev/null | grep -v Listing"} -
Executor Returns:
1curl/stable 7.81.0-1 amd64 [upgradable from: 7.74.0-1] -
AI Prompt: "Install the updates safely." โ Tool Call:
{"cmd": "apt-get update && apt-get upgrade -y"} -
Executor Runs Command
-
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