Bash, PowerShell, Python and jq for live exercises and fleet work
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start free-e exits when a command fails, -u errors on unset variables, -o pipefail makes a pipeline fail if any command in it fails, not just the last. Together they stop scripts from blindly continuing after an error.
The exit code of the last command: 0 for success, non-zero for failure. Scripts and monitoring use it to decide what happened.
Single quotes are literal: no variable or command expansion. Double quotes expand $var and $(cmd) but keep spaces intact. Use double quotes around variables almost always.
$1, $2... for positional args, $@ for all of them (quoted: "$@"), $# for the count, and ${1:-default} for a default value.
if ! systemctl is-active --quiet nginx; then systemctl restart nginx; logger "nginx restarted by watchdog"; fi. Then mention that systemd's Restart=on-failure usually makes this unnecessary, and that you'd alert rather than silently restart forever.
Ansible ad-hoc or a playbook (ansible all -m shell -a 'cmd' -f 30) for Linux, Invoke-Command -ComputerName for Windows. A bash loop with ssh -o BatchMode=yes -o ConnectTimeout=5 works in a pinch. Always test on one host first, then a small batch.
In PowerShell you filter and select properties directly (Where-Object Status -eq 'Running') without parsing columns. In bash you parse text with awk/cut, which breaks if output format changes. Objects make scripts more reliable and easy to export (CSV, JSON).
jq: e.g. jq -r '.items[].name'. For Azure CLI use --query with JMESPath and -o tsv for raw values. Don't grep JSON.
Bash for short glue around Linux commands. Python when there's logic, JSON/APIs, retries or it's growing past ~50 lines. Ansible when the job is 'make many servers look like this' and needs idempotency and inventory.
Idempotent checks before changes, a dry-run mode, strict error handling, timeouts, secrets from a vault, code review in Git, linting (shellcheck), and a staged rollout: one host, a canary group, then the rest, with a health check gate between stages and a documented rollback.