【发布时间】:2017-09-11 08:52:23
【问题描述】:
我正在尝试用属性文件中的值替换 html 文件中的 [[****]] 占位符。
输入文件中的示例内容:
<html>
<host>[[my_host]]</host>
<port>[[my_port]]</port>
</html>
属性文件中的示例内容:
my_host=linkcmb.com
my_port=8080
我当前的脚本:
#/bin/sh
property_file=$1
input_html=$2
output_html=$3
IFS="="
while read k v || [[ -n "$k" ]]; do
test -z "$k" && continue
declare $k=$v
done <"$property_file"
eval "$(sed 's/\[\[\([^]]\+\)\]\]/${\1}/g' $input_html) >$output_html";
错误:Html 标签也被评估导致错误。
./some.sh: line 32: html: No such file or directory
./some.sh: line 33: host: No such file or directory
./some.sh: line 35: /host: No such file or directory
....
....
任何建议将不胜感激。谢谢。
【问题讨论】:
-
为清晰起见添加示例输入行和预期输出...并且 sed 不是用于 html 文件的合适工具...
-
@Sundeep 更新问题,请检查
-
imo,这不适合 bash 脚本和 sed...我没有使用适当的 html 解析器的经验...对于给定的示例,
sed -f <(sed 's/^\([^=]*\)=\(.*\)/s|\\[\\[\1\\]\\]|\2|/' property.txt) ip.html可能有效,但它很容易打破实际用例 -
sh不是bash。
标签: linux bash shell sed scripting