The same script must run on engineers' macOS laptops and on Linux CI runners. What breaks, and how do you handle it?
IntermediateScenarioScenarios: writing and hardening scripts
Answer
Situation read → macOS ships Bash 3.2 and BSD userland; Linux has Bash 5 and GNU tools. Flags differ for sed -i, date -d, readlink -f, stat -c, grep -P, xargs -r.
What I'd do →
- Require Bash 4+ explicitly and tell macOS users to
brew install bash coreutils gnu-sed grep:(( BASH_VERSINFO[0] >= 4 )) || { echo "need Bash 4+ (brew install bash)" >&2; exit 1; } - Prefer portable forms:
sed -i.bak ... && rm *.bak,$(cd "$(dirname "$0")" && pwd)instead ofreadlink -f. - Where GNU behaviour is needed, detect and use
gdate,gsedwhen present. - Run CI on both
ubuntu-latestandmacos-latestif laptops are a supported target. - Or remove the problem: run the script inside a container image with pinned tools.
Why → "Works on my Mac" is the shell equivalent of "works on my machine".
Interview tip → Mention that #!/usr/bin/env bash picks up Homebrew's Bash on macOS, while #!/bin/bash pins Apple's 3.2.
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