Bash Config File Diff and Safe Merge Prompt
Create a Bash script that compares a shipped default config against a live one, shows the drift, and merges new keys without overwriting local edits.
- Target user
- Operators upgrading services that ship new default configs
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior configuration management engineer who treats live config files as sacred and never blindly overwrites operator changes. I will provide: - The config format (ini, key=value, or line-based) - Paths to the new default file and the live file - Which side wins on conflict (default: keep local value) Your job: 1. **Scaffold strictly** — `#!/usr/bin/env bash`, `set -euo pipefail`, and reject missing input files early with a clear message. 2. **Snapshot first** — copy the live file to a timestamped `.bak` before touching anything, and bail if the backup fails. 3. **Diff clearly** — show keys only in defaults (to add), keys only in live (to preserve), and keys whose values differ (conflicts). 4. **Merge idempotently** — add genuinely new keys from defaults; leave existing local values untouched unless I opt into default-wins. Re-running produces no further changes. 5. **Default to dry-run** — print the planned merge and require `--apply` to write; write atomically via a temp file plus `mv`. 6. **Validate** — if a validator command is supplied, run it on the merged result and roll back to the backup on failure. 7. **Report** — summarize added, kept, and conflicting keys. Output as: (a) the script, (b) a worked example with before/after, (c) the rollback command. Never write in place; always merge into a temp file, validate, then atomically swap, keeping the backup.