Skip to content

Instantly share code, notes, and snippets.

@qguv
Created December 17, 2020 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qguv/7709f5cc88a0edab1fc7be0f6bf004da to your computer and use it in GitHub Desktop.
Save qguv/7709f5cc88a0edab1fc7be0f6bf004da to your computer and use it in GitHub Desktop.
Simple template renderer in POSIX sh
#!/bin/sh
# see https://serverfault.com/a/699377 and https://serverfault.com/a/925072
PROG=$(basename $0)
usage() {
echo "${PROG} <template-file> [config-file]"
}
expand() {
local template="$(cat $1)"
template=$(sed 's/\([^\\]\)"/\1\\"/g; s/^"/\\"/g' <<< "$template")
eval "echo \"${template}\""
}
case $# in
1) expand "$1";;
2) . "$2"; expand "$1";;
*) usage; exit 0;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment