#!/bin/bash
# Creating Input parameters file
echo "------------------------------------------------------------"
KEY_VALUE_FILE=${TMPDIR}/key_value_input_file
echo "NAME=ron" > ${KEY_VALUE_FILE}
echo "AGE=NA" >> ${KEY_VALUE_FILE}
printf "Contents of the <${KEY_VALUE_FILE}> file is:\n"
cat ${KEY_VALUE_FILE}
echo "------------------------------------------------------------"
# Creating Template File
TEMPLATE=${TMPDIR}/template
echo 'Hello $(NAME), thank you for joining us' > ${TEMPLATE}
echo 'According to our records - you are $(AGE) years old, is that correct?' >> ${TEMPLATE}
printf "Contents of the <${TEMPLATE}> file is:\n"
cat ${TEMPLATE}
echo "------------------------------------------------------------"
# Start of "template engine"
TARGET=${TMPDIR}/target
cp ${TEMPLATE} ${TARGET}
while read line; do
KEY=$(echo $line | cut -d"=" -f1)
VALUE=$(echo $line | cut -d"=" -f2)
echo [DEBUG] replacing '$('${KEY}')' with $VALUE
sed -i.bck "s/\$(${KEY})/${VALUE}/g" ${TARGET}
done < ${KEY_VALUE_FILE}
# End of "template engine"
printf "Result (${TARGET}) file is:\n"
cat ${TARGET}
echo "------------------------------------------------------------"