Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for Bash & Python Automation Difficulty: Intermediate ClaudeChatGPT

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

More Bash & Python Automation prompts & error guides

Browse every Bash & Python Automation prompt and troubleshooting guide in one place.

Free download · 368-page PDF

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.