I need to make this script create and then output to an html file data inside template.vars. The script is a template engine, but right now all it does is take in input from the keyboard, echoing out the name in template.vars when the input is @NAME@. Here is the code of the template engine at the moment:
#!/bin/bash
IFS=@ #makes @ a delimiter.
while read line
do
dataspace=$(awk '$0=$1' FS== <<< "$line")
value=$(awk '$0=$2' FS== <<< "$line")
printf -v $dataspace "$value" #make the value stored in value into the name of a dataspace.
done < 'template.vars' #read template.vars for standard input.
skipflag=false #initialize the skipflag to false
while read line #while it is reading standard input one line at a time
do
read -a dataspacearray <<< "$line" #make the line into an array.
if [ $skipflag == false ] && [ "${dataspacearray[1]}" != "ENDIF" ] ; then
if [[ ${dataspacearray[1]} == "IF "* ]] ; then #If second element of dataspacearray is "IF "(something)
dataspace=`echo ${dataspacearray[1]} | cut -d' ' -f2`
if [ -z "${!dataspace}" ] ; then #if dataspace not found, skip everything up to endif. -z to test string
skipflag=true
fi
else
for ((i=0; i<${#dataspacearray[@]}; i++))
do
dataspace=${dataspacearray[i]} #access to each dataspace in the array
even=`expr $i % 2`
if [ $even == '0' ] ; then #if it's even(f0,f2, f4.. etc. etc) then it's not a variable, so print it directly
if [ -n "${dataspace}" ] ; then
printf ${dataspace}
fi
else #(odd dataspaces(f1, f3... etc.) are variables, print their values if they exist
if [ -n "${!dataspace}" ] ; then
printf ${!dataspace}
else
printf "Error!!!"
fi
fi
done
printf "\n"
fi
else
skipflag=false
fi
done
The html file goes in as input like this:
<html>
<head>
<title>@NAME@</title>
</head>
<body>
<p>Name: @NAME@</p>
<p>Major: @MAJOR@</p>
<p>Classification: @CLASS@</p>
<p>Graduation date: @GDATE@</p>
@IF HONORS@
<p>Graduating with Honors</p>
@ENDIF@
</body>
</html>
With the script replacing all strings beginning and ending with @ using the data in the template.vars file (or outputting error!!! if that isn't found). I have seen many instances where templates replace any text that has $ at the beginning, and think that I need to implement something similar, but with it reading the html file as input, and then saving it as output. How do I do this?