Created
December 17, 2020 13:35
-
-
Save qguv/7709f5cc88a0edab1fc7be0f6bf004da to your computer and use it in GitHub Desktop.
Simple template renderer in POSIX sh
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/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