Skip to content

Instantly share code, notes, and snippets.

@ryan-blunden
Created November 27, 2017 02:49
Show Gist options
  • Save ryan-blunden/93e406223b66f286e2504ccdf8c4b381 to your computer and use it in GitHub Desktop.
Save ryan-blunden/93e406223b66f286e2504ccdf8c4b381 to your computer and use it in GitHub Desktop.
Check for the existence of a list of variables. List missing vars and exit with code 1 if any are not set. They can however, be empty.
#!/usr/bin/env bash
# Check required environment vars are set
required_vars=(DJANGO_SECRET_KEY DB_NAME DB_USER DB_PASSWORD DB_HOST DB_PORT CACHE_HOST CACHE_PORT)
missing_vars=()
for i in "${required_vars[@]}"
do
test -n "${!i+set}" || missing_vars+=("$i")
done
if [ ${#missing_vars[@]} -gt 0 ]
then
echo "[error]: Aborting application start as required environment vars are not set:" >&2
printf ' - %q\n' "${missing_vars[@]}" >&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment