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).
Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 2,104 DevOps AI prompts
  • One practical workflow email per week