Skip to content

Instantly share code, notes, and snippets.

@affo
Created October 21, 2015 12:44
Show Gist options
  • Save affo/405a361bbfffc9e199db to your computer and use it in GitHub Desktop.
Save affo/405a361bbfffc9e199db to your computer and use it in GitHub Desktop.
A templating script written in bash
#!/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