Coding
AI Prompt for Porting Shell Scripts
Port shell scripts to Python with this AI prompt: same CLI behavior, proper error handling, and every shell-ism replaced with robust Python.
The prompt
Role: You are an automation engineer who ports shell scripts to maintainable [TO_LANGUAGE]. Context: From: [FROM_LANGUAGE]. To: [TO_LANGUAGE] ([VERSION]). CLI contract (must not change): [CLI_CONTRACT: flags, args, exit codes]. Script: [SCRIPT] External commands used: [EXTERNAL_COMMANDS] Task: 1) Port to idiomatic [TO_LANGUAGE]: proper argument parsing, structured error handling, logging instead of echo. 2) Replace shell-isms correctly: pipes → native constructs, set -e → exceptions, command substitution → subprocess with checked errors. 3) Keep [CLI_CONTRACT] byte-identical (flags, help text, exit codes). 4) List every behavioral nuance you preserved or intentionally changed (globbing, word splitting, signal handling). 5) Add a --dry-run mode if the script is destructive. Format: Ported script first, then the shell-ism replacements table, then behavioral nuances, then the dry-run note. Ask the AI to add a self-test mode that exercises each ported function against recorded outputs of the original script. Behavioral equivalence proven by execution beats equivalence argued by inspection. Constraints: Exit codes and CLI flags identical — scripts are APIs too. Every external command gets error checking; silent failures are forbidden. No shell=True equivalents without justification.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[FROM_LANGUAGE] [TO_LANGUAGE] [VERSION] [CLI_CONTRACT] [SCRIPT] [EXTERNAL_COMMANDS]
Example input
FROM=bash, TO=Python 3.11, SCRIPT=deploy script: builds, rsyncs, restarts service, 80 lines, KEEP_CLI=same flags and behavior
How to use this prompt
Keep the CLI contract fixed — users should not notice the port. List the external commands used; they become subprocess calls needing error handling.
Tips for the best results
- Test the port with --dry-run first on every destructive path.
- Compare exit codes between old and new on failure cases — callers depend on them.
- Ask for the behavioral-nuances list; shell semantics hide in word splitting and globs.
Frequently asked questions
Why port shell to Python?
It ports the logic to Python's strengths (subprocess, argparse, error handling) while keeping the CLI identical — then lists the shell-isms it replaced.
Is Python always better than shell for scripts?
For logic-heavy scripts, yes. For thin glue over CLI tools, shell is fine. The prompt ports faithfully either way — choose based on complexity.