【发布时间】:2017-01-30 18:39:38
【问题描述】:
我一直在网上关注一些 bash 教程。我在本教程here 中学习了 sed。本质上,一些文本被注入到 HTML 'pre' 元素中。
输入文本如下(命名为'test'):
line1
line2
line3
以下 HTML 模板保存为 shell 文件,并在主 shell 脚本中引用。模板看起来像这样(命名为script.sed):
1i\
<html>\
<head><title>sed generated html</title></head>\
<body bgcolor="#ffffff">\
<pre>
$a\ <!-- what is this variable? -->
</pre>\
</body>\
</html>
以下脚本从 shell 运行并引用“测试”文件作为输入(名为 txt2html.sh):
#!/bin/bash
# This is a simple script that you can use for converting text into HTML.
# First we take out all newline characters, so that the appending only happens
# once, then we replace the newlines.
echo "converting $1..."
SCRIPT="/home/sandy/scripts/script.sed"
NAME="$1"
TEMPFILE="/var/tmp/sed.$PID.tmp"
sed "s/\n/^M/" $1 | sed -f $SCRIPT | sed "s/^M/\n/" > $TEMPFILE
mv $TEMPFILE $NAME
echo "done."
脚本运行如下:
txt2html.sh test
输出是:
<html>
<head><title>sed generated html</title></head>
<body bgcolor="#ffffff">
<pre>
line1
line2
line3
</pre>
</body>
</html>
script.sed 文件中的$a 变量是什么?我以前从未遇到过$a。在这种情况下它是如何工作的?
【问题讨论】: