Created
July 21, 2018 21:57
-
-
Save pascal08/0b1909cfa385f6d960724fc6667544be to your computer and use it in GitHub Desktop.
Parse environment placeholders (useful when using Docker for example)
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 | |
# Replace variables $ENV{<environment varname>} | |
# (optional: $ENV{<environment varname>??<default value>} | |
# ---- | |
function ReplaceEnvironmentVariable() { | |
# - Parse environment variable placeholders. | |
# - The format should be $ENV{***} where *** should match | |
# the exact name of the environment variable. | |
# - Use ?? to specify a default value in case the | |
# environment variable was not found. | |
# - Regex characters should be escaped before parsing! | |
grep -rl "\$ENV{$1\(??[^}]*\)\?}" $3|xargs -r \ | |
sed -i "s/\$ENV{$1[^}]*}/$2/g" | |
} | |
function DefaultEnvironmentVariable() { | |
REGEXP="\$ENV{[^??]*??\([^}]*\)}" | |
grep -rl -- "$REGEXP" $1|xargs -r \ | |
sed -i "s/$REGEXP/\1/g" | |
} | |
# Replace all variables | |
for _curVar in `env | awk -F = '{print $1}'`;do | |
# - 'awk' has split them by the equals sign. | |
# - Pass the name and value to our function | |
# - Works only in environments supporting indirect expansion like bash | |
ReplaceEnvironmentVariable ${_curVar} ${!_curVar} /etc/nginx/conf.d/* | |
ReplaceEnvironmentVariable ${_curVar} ${!_curVar} /etc/nginx/sites-available/* | |
DefaultEnvironmentVariable /etc/nginx/conf.d/* | |
DefaultEnvironmentVariable /etc/nginx/sites-available/* | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment