1

I want to include some HTML in a shell script. This is what I've tried:

(
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:

> 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?

that other guy
  • 116,971
  • 11
  • 170
  • 194
par181
  • 401
  • 1
  • 11
  • 29

4 Answers4

4

A better option would be to use the here document syntax (see this answer):

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 echo command.

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.

that other guy
  • 116,971
  • 11
  • 170
  • 194
eudoxos
  • 18,545
  • 10
  • 61
  • 110
1

You need to escape the quote in the html, because you have a quote on the start of the argument to 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 style=\"font-family:verdana;color:red;\">
abresas
  • 835
  • 6
  • 7
1

Read the Advanced Bash-Scripting Guide Chapter 19. Here Documents. 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
Stephane Rouberol
  • 4,286
  • 19
  • 18
-1

You can use the online tool to do the same thing:

http://togotutor.com/code-to-html/shell-to-html.php