Skip to main content

Claude Prompt to Generate a Bash Script with Error Handling

Generate production-ready Bash scripts with strict-mode error handling, logging, argument parsing, and cleanup traps for any automation task.

Füllen Sie die Platzhalter aus

Edit the values, then copy your finished prompt.

Ihr Prompt
prompt.txt

                                

What this prompt does

This prompt generates a production-grade Bash script that performs a [script_purpose], runs on [target_os], and handles [input_type]. It encodes a real hardening checklist: strict mode with set -euo pipefail, a cleanup trap on EXIT, INT, and TERM, argument parsing with --verbose, --dry-run, --config, and --help, structured logging to stdout and [log_file], input validation, progress indicators, and exponential-backoff retries for [retry_operations].

The variables steer what the script automates and how defensively it behaves. [script_purpose] and [input_type] define the core task, [log_file] sets where structured logs land, and [retry_operations] tells the model which steps are flaky enough to wrap in retry logic. The fixed exit-code summary (0 success, 1 general, 2 missing dependencies, 3 permission denied) makes the script's outcomes scriptable by callers.

When to use it

  • Writing automation that must survive on a production box without leaving a mess
  • Backup or deploy scripts where a half-finished run leaving temp files or stale locks is dangerous
  • You want strict mode and a cleanup trap as non-negotiable defaults
  • Adding structured, timestamped logging with INFO, WARN, and ERROR levels
  • Operations like [retry_operations] that need exponential backoff on transient failure
  • You need consistent exit codes so a calling system can react to failures

Example output

You get a single well-commented Bash script: a strict-mode header, a usage function, getopts-based flag parsing, a logging helper writing to stdout and [log_file], a validation block checking files, permissions, disk space, and dependencies via command -v, the core logic for [script_purpose], a retry function, and a final exit-code summary. Inline comments explain the non-obvious idioms.

Pro tips

  • Make [script_purpose] specific (for example "back up PostgreSQL to S3 with compression and encryption") so validation and retry logic target the right steps
  • Always test the --dry-run path first; a destructive [script_purpose] is exactly where you want to preview actions before running for real
  • Point [log_file] somewhere writable by the run user, or add a fallback — a log path you cannot write to defeats the structured logging
  • Wrap only genuinely flaky steps in retries via [retry_operations]; retrying a logic error just delays the failure
  • Verify the cleanup trap covers temp files and locks; set -e can exit mid-run, and the trap is what prevents stale state
  • Keep the exit-code contract intact so cron or a CI job can distinguish a missing dependency (2) from a permission problem (3)
  • Validate dependencies up front with command -v checks so the script fails fast with a clear message instead of dying halfway through with a cryptic command-not-found
  • Use the structured logging levels deliberately: reserve ERROR for conditions that abort the run, and let WARN cover recoverable issues, so scanning [log_file] after a failure points straight at the cause rather than burying it in noise

Frequently Asked Questions

What does set -euo pipefail actually protect against?
It makes the script exit on the first error (-e), treat unset variables as errors (-u), and fail a pipeline if any stage fails (-o pipefail). Together they stop a script from blindly continuing after a failed command, which is the most common cause of partial, damaging runs in automation.
Does the generated script clean up temp files if it fails midway?
Yes. The prompt defines a cleanup trap on EXIT, INT, and TERM that removes temporary files regardless of how the script ends. This matters because strict mode can exit mid-run, and without the trap you would leave stale temp files or locks behind.
Can I preview what the script will do before it changes anything?
Yes, through the `--dry-run` flag the prompt requires in its argument parsing. For a destructive `[script_purpose]` like a backup or deploy, running with `--dry-run` first lets you confirm the planned actions without touching real data.
Why does it define specific exit codes?
The fixed codes (0 success, 1 general error, 2 missing dependencies, 3 permission denied) let a calling system such as cron or CI react differently to each failure type. Generic non-zero exits force the caller to guess what went wrong, whereas distinct codes make the failure reason machine-readable.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

Mehr in Linux & Shell Scripting Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support