2

How do we create html file in shell script?

I tried the below code

    echo "<html>"
    echo "<head>"
    echo "<style type=\"text/css\">"
    echo "table{background-color:#DCDCDC}"
    echo "thead {color:#708090}"
    echo "tbody {color:#191970}"
    echo "</style>"
    echo "</head>"
    echo "<body>"
    echo "<table border=\"1\">"
    echo "<thead>"
    echo "<tr width="100" bgcolor='#C0C0C0'><center><td colspan="3"><font color="#000000"><b>CONSOLIDATED RUNBOOK</b></font></center></td>"
    echo "</h4>"
    echo "</tr>"
    echo "<tr>"
    echo "<th><col width="100"><font color="000000">APP NAME</font></th>"
    echo "<th><col width="100"><font color="000000">STATUS</font></th>"
    echo "<th><col width="100"><font color="000000">STATUS AS ON</font></th>"
    echo "</tr>"
 >> text.html

I am trying to create html file with the help of shell script. Can you please help>

Arpit Rathod
  • 91
  • 1
  • 4
  • 11
  • 2
    If you're using `"` for your echo statements you can't use `"` *within* the string unless you escape each instance with backslash \ Why not use some kind of template system instead of this dizzying pile of `echo` statements? Using something primitive like `sed` would be better than this. – tadman May 14 '18 at 17:34
  • 1
    Or a [here document](https://www.gnu.org/software/bash/manual/bash.html#Here-Documents). – Benjamin W. May 14 '18 at 17:42
  • Possible duplicate of [Open and write data on text file by bash/shell scripting](https://stackoverflow.com/questions/11162406/open-and-write-data-on-text-file-by-bash-shell-scripting) – Benjamin W. May 14 '18 at 17:42

2 Answers2

1
{
echo "<html>"
echo "<head>"
echo "<style type=\"text/css\">"
echo "table{background-color:#DCDCDC}"
echo "thead {color:#708090}"
echo "tbody {color:#191970}"
echo "</style>"
echo "</head>"
echo "<body>"
echo "<table border=\"1\">"
echo "<thead>"
echo "<tr width="100" bgcolor='#C0C0C0'><center><td colspan="3"><font color="#000000"><b>CONSOLIDATED RUNBOOK</b></font></center></td>"
echo "</h4>"
echo "</tr>"
echo "<tr>"
echo "<th><col width="100"><font color="000000">APP NAME</font></th>"
echo "<th><col width="100"><font color="000000">STATUS</font></th>"
echo "<th><col width="100"><font color="000000">STATUS AS ON</font></th>"
echo "</tr>"
}>> text.html
Arpit Rathod
  • 91
  • 1
  • 4
  • 11
1

You could use a heredoc instead:

cat > text.html <<EOF
<html>
<head>
<style type=\"text/css\">
table{background-color:#DCDCDC}
thead {color:#708090}
tbody {color:#191970}
</style>
</head>
<body>
<table border=\"1\">
<thead>
<tr width="100" bgcolor='#C0C0C0'><center><td colspan="3"><font color="#000000"><b>CONSOLIDATED RUNBOOK</b></font></center></td>
</h4>
</tr>
<tr>
<th><col width="100"><font color="000000">APP NAME</font></th>
<th><col width="100"><font color="000000">STATUS</font></th>
<th><col width="100"><font color="000000">STATUS AS ON</font></th>
</tr>
EOF
bathyscapher
  • 1,615
  • 1
  • 13
  • 18