Validate that required environment variables are set before a deploy script runs.
IntermediateHands-onBash scripting tasks
Answer
required=(AWS_REGION CLUSTER_NAME IMAGE_TAG)
missing=0
for v in "${required[@]}"; do
if [[ -z "${!v:-}" ]]; then echo "Missing env var: $v" >&2; missing=1; fi
done
(( missing )) && exit 1
Shorter for one variable: : "${IMAGE_TAG:?IMAGE_TAG must be set}".
Key trick → ${!v} is indirect expansion, meaning "the value of the variable whose name is in $v". The :- stops set -u from aborting on it.
More on Bash scripting tasks
Write the safe header you start every Bash script with, and explain each part.BeginnerWrite a script that alerts when any filesystem is above 80% usage.BeginnerParse an Nginx access log and print the top 10 client IPs by request count.BeginnerWrite a backup script that tars a directory with a timestamp and keeps only the last 7 backups.IntermediateWrite a health-check loop that polls a URL until it returns 200, with a timeout.IntermediateParse command-line flags `-e <env>`, `-v` (verbose) and `-h` in a Bash script.Intermediate