Created
October 21, 2015 12:44
-
-
Save affo/405a361bbfffc9e199db to your computer and use it in GitHub Desktop.
A templating script written in bash
This file contains 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 | |
if [[ $# -eq 0 ]]; then | |
echo "Usage:" | |
echo " templater key=value ... inputfile" | |
echo | |
echo "Sample input file:" | |
echo " Hello \${name}, enjoy templater!" | |
echo | |
echo "Sample output:" | |
echo " $ ./templater name=Affo input" | |
echo " Hello Affo, enjoy templater!" | |
echo | |
exit | |
fi | |
args=( "$@" ) | |
fname=${args[${#args[@]}-1]} | |
unset "args[${#args[@]}-1]" | |
cmd="sed" | |
for arg in "${args[@]}"; do | |
if [[ "$arg" != *"="* ]]; then | |
continue | |
fi | |
key="${arg%=*}" | |
value="${arg#*=}" | |
cmd="$cmd -e 's/\${$key}/$value/g'" | |
done | |
cmd="$cmd $fname" | |
eval $cmd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment