templater.sh: use perl if available (#98)

This commit is contained in:
Manuel Streuhofer 2018-01-25 10:35:53 +01:00 committed by Kevin van Zonneveld
parent f57e37eb68
commit ee869a9584

View File

@ -39,12 +39,17 @@ function templater() {
exit 1 exit 1
fi fi
if [[ "$(command -v perl)" ]]; then
perl -p -e 's/\$\{(\w+)\}/(exists $ENV{$1} ? $ENV{$1} : "\${$1}")/eg' < "${templateSrc}" > "${templateDst}"
else
cp -f "${templateSrc}" "${templateDst}" cp -f "${templateSrc}" "${templateDst}"
for var in $(env |awk -F= '{print $1}' |grep -E '^(_[A-Z0-9_]+|[A-Z0-9][A-Z0-9_]*)$'); do for var in $(env |awk -F= '{print $1}' |grep -E '^(_[A-Z0-9_]+|[A-Z0-9][A-Z0-9_]*)$'); do
sed -i.bak -e "s#\${${var}}#${!var//#/\\#/}#g" "${templateDst}" sed -i.bak -e "s#\${${var}}#${!var//#/\\#/}#g" "${templateDst}"
# this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666 # this .bak dance is done for BSD/GNU portability: http://stackoverflow.com/a/22084103/151666
rm -f "${templateDst}.bak" rm -f "${templateDst}.bak"
done done
fi
# cat "${templateDst}" # cat "${templateDst}"