0

このスクリプトを作成してから、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 ファイルを入力として読み取り、出力として保存します。どうすればいいですか?

4

1 に答える 1

1

これは厳密にはあなたが求めたものではありませんが、おそらく、何か作業を行うには十分に近いものです。

@IF ...@ ... @ENDIF@ 処理 (ほとんどテストされていない後付け) を脇に置いておくと、ロジックは次のようになります。

入力行ごとに

1) 空の保持スペースを初期化する

2) 行の区切り文字が 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
于 2014-04-12T12:45:48.250 に答える