[英]How do I make this bash script create an html file using the input from the template.vars file?
我需要創建此腳本,然后將其輸出到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文件作為輸入,然后將其保存為輸出。 我該怎么做呢?
嚴格來說,這並不是您所要的,但它可能足夠接近您可以使用的內容。
撇開@IF ... @ ... @ ENDIF @處理(這是未經測試的事后思考),邏輯是這樣的:
對於每條輸入線
1)初始化一個空的容納空間
2)如果該行的分隔符少於兩個,則轉到#10
3)從行的開頭復制到(但不包括)第一個定界符到保留空間
4)刪除該行的第一段(包括定界符)。 該行的開頭部分現在應該是令牌。
5)將令牌(直到定界符)復制到一個臨時變量中
6)從行中刪除令牌(保留尾隨定界符)
7)在已知令牌列表中查找令牌。
8)如果它是已知令牌,則將令牌的擴展名附加到保留空間,然后從行中刪除定界符。 如果令牌未知,則將分隔符(在#4處刪除)和令牌文本附加到保留空間,但在行的開頭保留非令牌的尾隨定界符-這是允許的(@name @@@ domain @)和使用不同的變量定義文件(./expstuff -v user.vars <template.in | ./expstuff -v global.vars> outputfile)進行多次運行,但需要根據您的要求進行更改。
9)轉到#2
10)打印保留空間和行的其余部分。
typeset -A s
i=template.vars
d=@
while IFS='=' read t v ; do
[[ $t != \#* ]] && s[$t]=$v
done < "$i"
while IFS= read -r l ; do
# assuming the @IF blah@ and corresponding @ENDIF@ occupy entire lines.
if [[ $l == $d[Ee][Nn][Dd][Ii][Ff]$d ]]; then
c=''
continue
fi
[[ $c == '0' ]] && continue
if [[ $l == $d[Ii][Ff][[:space:]]*[![:space:]]*$d ]]; then
read _ t <<< "${l%$d}"
c=0
if [[ -n $t && ${s[$t]+t} == 't' ]]; then
case ${s[$t]} in [Yy]|[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1 ) c=1 ;; esac
fi
continue
fi
# Currently, given
# foo@domain@TLD@ wibble
# with TLD=.co.uk
# the following loop outputs
# foo@domain.co.uk wibble
# You appear to require
# fooError!!!TLD@wibble
h=
while [[ $l == *$d*$d* ]]; do
h=$h${l%%$d*}
l=${l#*$d}
t=${l%%$d*}
l=${l#$t}
if [[ -n $t && ${s[$t]+t} == 't' ]]; then
h=$h${s[$t]}
l=${l#$d}
else
h=$h$d$t
# for apparent requirement change preceding line to (untested)
# h=$h"Error!!!" ; l=${l#$d}
fi
done
printf '%s\n' "$h$l"
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.