-
-
Save vindard/cd0309f765627a1f7bdc610973b61e60 to your computer and use it in GitHub Desktop.
A bash script that can source its required environment variables from an env file and re-execute itself
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export TEST_VAL_A=1 | |
export TEST_VAL_B=2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# -------------- | |
# Pre-run environment variable checks | |
if [[ -z $REQUIRED_ENVS ]]; then | |
echo "'REQUIRED_ENVS' array is required to run pre-checks, exiting..." | |
exit 1 | |
fi | |
SCRIPT_NAME="$0" | |
ENV_FILENAME=".env" | |
PARENT_DIR=$(dirname $SCRIPT_NAME) | |
ENV_FILE="$PARENT_DIR/$ENV_FILENAME" | |
check_envs () { | |
echo "Running env variables check..." | |
for env_var in "$@" ; do | |
if [[ -z "${!env_var}" ]]; then | |
echo "Error: '$env_var' required env variable not set or has empty value" | |
echo | |
return 1 | |
fi | |
done | |
echo "All required envs set" | |
echo | |
} | |
check_env_file () { | |
if [[ ! -e $ENV_FILE ]]; then | |
echo "Env file '$ENV_FILE' does not exist, exiting..." | |
exit 1 | |
fi | |
echo "Checking '$ENV_FILE' for required envs..." | |
for env_var in "$@" ; do | |
if ! cat $ENV_FILE | grep -q "\b$env_var\b"; then | |
echo "Error: '$env_var' required env variable not found in '$ENV_FILE'" | |
echo | |
return 1 | |
fi | |
done | |
echo "All required envs found in '$ENV_FILE'" | |
} | |
source_env_and_rerun () { | |
if [[ -e $ENV_FILE ]]; then | |
source $ENV_FILE | |
$SCRIPT_NAME | |
exit | |
else | |
echo "Env file '$ENV_FILE' does not exist, exiting..." | |
exit 1 | |
fi | |
} | |
check_env_before_run () { | |
if ! check_envs ${REQUIRED_ENVS[@]}; then | |
if check_env_file ${REQUIRED_ENVS[@]}; then | |
echo "Attempting source from $ENV_FILE" | |
echo | |
source_env_and_rerun | |
else | |
echo "Missing envs not found in '$ENV_FILE', exiting..." | |
exit 1 | |
fi | |
fi | |
} | |
check_env_before_run && \ | |
echo "----------" && \ | |
echo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Do env variables check 1st | |
REQUIRED_ENVS=(\ | |
"TEST_VAL_A" \ | |
"TEST_VAL_B" \ | |
# "TEST_VAL_C" \ | |
) | |
source check_env.sh | |
# -------------- | |
# Actual script starts here | |
echo "Hello World:" | |
for env_var in ${REQUIRED_ENVS[@]}; do | |
echo " $env_var=${!env_var}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment