A Bash script has grown to 900 lines, parses JSON in 40 places and has nested associative-array tricks. Your manager asks whether to keep it. What do you say?
IntermediateScenarioScenarios: writing and hardening scripts
Answer
Situation read → The script has outgrown the language. Bash is strong at orchestrating commands, weak at data structures and testable logic.
What I'd do →
- Identify the parts that are glue (calling
kubectl,aws,rsync) and the parts that are logic (JSON transforms, decisions). - Propose moving the logic to Python (or Go if a static binary helps distribution), with unit tests, while keeping a thin Bash wrapper if operators like the interface.
- Migrate incrementally: one subcommand at a time, both versions tested against the same fixtures.
- Until then, add ShellCheck and bats around the current script so the migration has a safety net.
Why → Maintenance cost and bug rate grow sharply when Bash is used as a general-purpose language.
Interview tip → Give a rule of thumb, such as "more than about 300 lines, or real data structures, means Python".
More on Scenarios: writing and hardening scripts
You inherit a 600-line `deploy.sh` with no error handling. It "sometimes half-deploys". Where do you start?IntermediateA script with `#!/bin/sh` works on CentOS but fails on Ubuntu with `Syntax error: "(" unexpected`. What is going on and what do you do?BeginnerYou need a deploy script that humans and CI both call with `--env`, `--version` and `--dry-run`. How do you design its interface?BeginnerA maintenance script must run as the `deploy` user, never as root, because files it creates must be owned by `deploy`. How do you enforce that?BeginnerTen scripts in a repo each copy-paste their own `log`, `die`, retry and config loading. How do you fix this?IntermediateA script is run both by engineers and by cron. Cron logs are full of colour codes and the script sometimes hangs waiting for input. Fix it.Beginner