bash 如何在 shell 脚本中嵌入一些 HTML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12325144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I embed some HTML in a shell script?
提问by par181
I want to include some HTML in a shell script. This is what I've tried:
我想在 shell 脚本中包含一些 HTML。这是我尝试过的:
(
echo "<html>
<head>
<title>HTML E-mail</title>
</head>
<body>
<p style="font-family:verdana;color:red;">
This text is in Verdana and red</p>
</body>
</html>"
)>pkll.htm
However, instead of writing the HTML to file, it gives me some errors:
但是,它没有将 HTML 写入文件,而是给了我一些错误:
> bash: color:red: command not found bash: > This text is in Verdana and
> red</p </body> </html>: No such file or directory
How can I do this?
我怎样才能做到这一点?
回答by eudoxos
A better option would be to use the here document syntax (see this answer):
更好的选择是使用 here 文档语法(请参阅此答案):
cat << 'EOF' > pkll.htm
<html>
<head>
<title>HTML E-mail</title>
</head>
<body>
<p style="font-family:verdana;color:red;">
This text is in Verdana and red
</p>
</body>
</html>
EOF
Your attempt failed because the double quotes in the HTML terminates the double quotes you wrapped around it and causing the <>s to be seen as redirections and the ;s to terminate the echocommand.
您的尝试失败了,因为 HTML 中的双引号终止了您环绕它的双引号,并导致<>s 被视为重定向,而;s 终止了echo命令。
You could technically have used single quotes:
从技术上讲,您可以使用单引号:
(
echo '<html>
<head>
<title>HTML E-mail</title>
etc ...'
)>pkll.htm
but then you just have the same problem again if the HTML contains a ', such as an apostrophe or in an attribute. The here document has no such issues.
但是,如果 HTML 包含',例如撇号或属性,您就会再次遇到相同的问题。这里的文档没有这样的问题。
回答by abresas
You need to escape the quote in the html, because you have a quote on the start of the argument to echo.
您需要对 html 中的引号进行转义,因为您在要 echo 的参数的开头有一个引号。
Your terminal interprets it as
您的终端将其解释为
<html>...<p style="
first argument
第一个论点
font-family:verdana;
as second argument, and the rest as other commands because you have a semicolon.
作为第二个参数,其余的作为其他命令,因为你有一个分号。
So you need to replace the p tag into
所以你需要把p标签替换成
<p style=\"font-family:verdana;color:red;\">
回答by Stephane Rouberol
Read the Advanced Bash-Scripting Guide Chapter 19. Here Documents. http://tldp.org/LDP/abs/html/here-docs.html
阅读高级 Bash 脚本指南第 19 章。这里是文档。http://tldp.org/LDP/abs/html/here-docs.html
cat << 'EOF' > pkll.htm
<html>
<head>
<title>HTML E-mail</title>
</head>
<body>
<p style="font-family:verdana;color:red;">
This text is in Verdana and red</p>
</body>
</html>
EOF
回答by user2378669
You can use the online tool to do the same thing:
您可以使用在线工具来做同样的事情:

