このスクリプトを作成してから、template.vars 内のデータを html ファイルに出力する必要があります。このスクリプトはテンプレート エンジンですが、現在はキーボードからの入力を取り込み、入力が @NAME@ の場合は template.vars に名前をエコーアウトするだけです。現時点でのテンプレート エンジンのコードは次のとおりです。
#!/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
html ファイルは、次のように入力として入ります。
<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>
スクリプトで、template.vars ファイルのデータを使用して @ で始まるすべての文字列を置き換えます (または、見つからない場合はエラーを出力します!!!)。テンプレートが最初に $ を持つテキストを置き換える多くの例を見てきましたが、同様のものを実装する必要があると思いますが、html ファイルを入力として読み取り、出力として保存します。どうすればいいですか?