Skip to content

Instantly share code, notes, and snippets.

@qguv

qguv/template.sh

Created Dec 17, 2020
Embed
What would you like to do?
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