fork(2) download
  1. #!/bin/bash
  2. # Creating Input parameters file
  3. echo "------------------------------------------------------------"
  4. KEY_VALUE_FILE=${TMPDIR}/key_value_input_file
  5. echo "NAME=ron" > ${KEY_VALUE_FILE}
  6. echo "AGE=NA" >> ${KEY_VALUE_FILE}
  7. printf "Contents of the <${KEY_VALUE_FILE}> file is:\n"
  8. cat ${KEY_VALUE_FILE}
  9. echo "------------------------------------------------------------"
  10. # Creating Template File
  11. TEMPLATE=${TMPDIR}/template
  12. echo 'Hello $(NAME), thank you for joining us' > ${TEMPLATE}
  13. echo 'According to our records - you are $(AGE) years old, is that correct?' >> ${TEMPLATE}
  14. printf "Contents of the <${TEMPLATE}> file is:\n"
  15. cat ${TEMPLATE}
  16. echo "------------------------------------------------------------"
  17.  
  18. # Start of "template engine"
  19. TARGET=${TMPDIR}/target
  20. cp ${TEMPLATE} ${TARGET}
  21.  
  22. while read line; do
  23. KEY=$(echo $line | cut -d"=" -f1)
  24. VALUE=$(echo $line | cut -d"=" -f2)
  25. echo [DEBUG] replacing '$('${KEY}')' with $VALUE
  26. sed -i.bck "s/\$(${KEY})/${VALUE}/g" ${TARGET}
  27. done < ${KEY_VALUE_FILE}
  28. # End of "template engine"
  29.  
  30. printf "Result (${TARGET}) file is:\n"
  31. cat ${TARGET}
  32. echo "------------------------------------------------------------"
Success #stdin #stdout 0s 5040KB
stdin
Standard input is empty
stdout
------------------------------------------------------------
Contents of the </tmp/bsXxoS/key_value_input_file> file is:
NAME=ron
AGE=NA
------------------------------------------------------------
Contents of the </tmp/bsXxoS/template> file is:
Hello $(NAME), thank you for joining us
According to our records - you are $(AGE) years old, is that correct?
------------------------------------------------------------
[DEBUG] replacing $(NAME) with ron
[DEBUG] replacing $(AGE) with NA
Result (/tmp/bsXxoS/target) file is:
Hello ron, thank you for joining us
According to our records - you are NA years old, is that correct?
------------------------------------------------------------