Created
April 25, 2018 20:56
-
-
Save jnovack/da459ea08fc9f4202b69e5b66baac6e5 to your computer and use it in GitHub Desktop.
Variables of Variables in /bin/sh
This file contains 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/sh | |
file_env() { | |
local envVar="$1" | |
local fileVar="${envVar}_FILE" | |
eval envVarContents="\$${envVar}" | |
eval fileVarContents="\$${fileVar}" | |
if [ ! -z "$envVarContents" ] && [ ! -z "$fileVarContents" ]; then | |
echo >&2 "error: both $envVar and $fileVar are set (but are exclusive)" | |
exit 1 | |
fi | |
local val | |
if [ ! -z "${envVarContents}" ]; then | |
val="${envVarContents}" | |
elif [ ! -z "${fileVarContents}" ]; then | |
val=`cat ${fileVarContents}` | |
elif [ ! -z "$2" ]; then | |
val=$2 | |
fi | |
export "$envVar"="$val" | |
unset "$fileVar" | |
} | |
########################### | |
## Example Script Use | |
########################### | |
file_env 'TEST' foo | |
echo $TEST | |
########################### | |
## Example Command Line Use | |
########################### | |
# $ echo 'hi' > /myenv | |
# $ ./file_env.sh | |
# foo | |
# $ TEST=asdf ./file_env.sh | |
# asdf | |
# $ TEST_FILE=/myenv ./file_env.sh | |
# hi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use this to get non-critical Docker Secrets into environment variables.
Example:
mariadb
image to specifyMYSQL_ROOT_PASSWORD_FILE