Skip to content

Instantly share code, notes, and snippets.

@spacelis
Created April 22, 2015 09:18
Show Gist options
  • Save spacelis/fc1d449bc0cbab342336 to your computer and use it in GitHub Desktop.
Save spacelis/fc1d449bc0cbab342336 to your computer and use it in GitHub Desktop.
#!/bin/bash
## This is a simple templating script based and bash/sed.
# Each templates should start with #!/sbin/render_template <dest>.
# The rendered will be save at <dest>.
# The values are provided via environment variables just like normal shell script.
# Those names in double curly braces will be replaced with corresponding value while the likes of $name, $path will left untouched.
# This rule is for escaping $host, $scheme in nginx config files.
# As the templates can be used as normal script, it is better to be placed in your my_init.d file or the like.
#
# MIT LICENSE by Wen Li Copyright 2015
function print_usage {
echo "Usage: $(basename $0) [-e <env.sh>] <dst> <template>"
}
echo "$@"
if [ $# -lt 1 ]; then
print_usage
exit 1
fi
# Ensure the dir is available
mkdir -p $(dirname "$file")
_cmd__asdf="echo -e \"$(cat $template | sed -e 's/\$\(\w\+\)/\\$\1/' -e 's/{{\(\w\+\)}}/${\1}/' | tail -n+2 )\" > $file "
if [ -f "${env_script:-}" ]; then
source "${env_script}"
fi
# Processing the template and generate the file to the given path
eval "echo -e \"$(cat $2 | sed -e 's/\$\(\w\+\)/\\$\1/g' -e 's/{{\(\w\+\)}}/${\1}/g' | tail -n+2 )\" > $1 "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment