sedなしでそれを行う方法は次のとおりです。
まず、プレースホルダーが bash 変数であるわずかに変更されたテンプレート ファイル:
blah blah blah
blah $PLACEHOLDER_1
blah
$PLACEHOLDER_2
そしてスクリプト:
#! /bin/sh
templatefile=output.template
outputfile=output.txt
PLACEHOLDER_1='string 1'
PLACEHOLDER_2='multiline
string
2'
# DONE: Generate file output.txt from file output.template
# using placeholders above.
echo "$(eval "echo \"$(cat $templatefile)\"")" > $outputfile
これは、スクリプト内に含まれるテンプレートを示すバージョンですが、ひねりがあります。また、テンプレート ファイル バージョンでも使用できるデフォルト値を示します。また、テンプレートで計算を行うこともできます。
#! /bin/sh
template='blah blah blah
blah $PLACEHOLDER_1
blah
${PLACEHOLDER_2:-"some text"} blah ${PLACEHOLDER_3:-"some
lines
of
text"} and the total is: $((${VAL_1:-0} + ${VAL_2:-0}))'
# default operands to zero (or 1) to prevent errors due to unset variables
outputfile=output.txt
# gears spin, bells ding, values for placeholders are computed
PLACEHOLDER_1='string 1'
PLACEHOLDER_2='multiline
string
2'
VAL_1=2
VAL_2=4
unset PLACEHOLDER_3 # so we can trigger one of the defaults
# Generate file output.txt from variable $template
# using placeholders above.
echo "$(eval "echo \"$template\"")" > $outputfile
sed もループもありません。毛むくじゃらのネスティングと引用符だけです。すべての引用がテンプレート ファイル内の悪意のあるものから保護すると確信していますが、それを保証するつもりはありません。