Convert a Fragile Bash Pipeline to Python Prompt
Rewrite an overgrown, fragile Bash script full of pipes, subshells, and string parsing into a maintainable Python program with proper error handling, types, and tests.
- Target user
- Automation engineers maintaining legacy shell scripts
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior automation engineer who ports unmaintainable Bash to clean, testable Python. The goal is equivalent behavior with far better failure visibility, not a clever rewrite. I will paste: - The existing Bash script (with its pipes, `awk`/`sed`/`grep` chains, and any traps) - How it is invoked today (cron, CI, manual) and what its inputs/outputs are - The target Python version and whether third-party packages are allowed Your job: 1. **Map behavior first** — produce a short table of every external command and side effect the script performs, so nothing silent is lost in translation. 2. **Translate, don't transliterate** — replace pipe-and-parse chains with native Python (`pathlib`, `csv`, `json`, `re`) where it's clearer; keep genuine external tools but call them via `subprocess.run([...], check=True)` with list args (never `shell=True` on untrusted input). 3. **Replace silent failure** — every place the Bash version ignored exit codes, surface it: raise, log at WARNING/ERROR, or return a typed result. 4. **Add structure** — split into a `main()` with `argparse`, pure helper functions, and a `if __name__ == "__main__"` guard returning a real exit code. 5. **Preserve the contract** — match stdout/stderr separation, exit codes, and any output format that downstream consumers parse. 6. **Add tests** — write `pytest` cases for the core transform plus one for the failure path you fixed. Output: the full Python module, a behavior-parity checklist mapping old lines to new functions, the test file, and a note on any intentional behavior change (with justification).
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Asyncio TaskGroup Structured-Concurrency Python Script Prompt
Write a Python 3.11+ automation script that fans work out with asyncio.TaskGroup so failures cancel siblings cleanly, errors aggregate into an ExceptionGroup, and no task is ever silently orphaned.
-
Add Pytest Coverage to an Automation Script Prompt
Wrap an existing Python automation script in a pytest suite that mocks subprocess, network, filesystem, and clock side effects so the logic is tested fast and deterministically without touching real systems.
-
Idempotent Bulk File Rename & Migrate Tool Prompt
Write a Python tool that safely renames or relocates large batches of files using a rule set, with mandatory dry-run, collision detection, atomic moves, and an undo manifest so a bad run is reversible.
-
Python Log Parser & Anomaly Extractor Prompt
Build a Python tool that streams large or rotated log files, parses structured and semi-structured lines, aggregates error patterns, and emits a summary report without loading everything into memory.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.