首页 > 解决方案 > 如何绘制包含 SQL 查询结果的 HTML 表并在 Shell 脚本中发送到邮件

问题描述

我想绘制包含 SQL 查询记录的 HTML 表并在 shell 脚本中发送到邮件。这是shell脚本: -

#!/usr/bin/ksh
out=$(for id in 1 2 3 4
      do
      sqlplus -s <user>/<password>@<db> <<EOF
      spool out.txt;
      select id, count(*) from <table> where id=${id};
      spool off;
      exit;
      EOF
      done)

 echo ${out} > out.txt
(uuencode out.txt out.txt)| cat out.txt| mailx -s "Before" abc@xyz.com

而且我得到了不同格式的查询输出,而不是 HTML 表格格式。表格格式应该是这样的(在有边框线的表格中)

ID            Results
        Before      After
1          12         15
2          27         30 
3          45         52

你能帮我画出这条记录的HTML表格吗?

标签: htmloracleshellunix

解决方案


现在,我在我的环境中重现了这个问题

#!/usr/bin/ksh

#start table
echo "<table><thead><tr><th>ID</th><th>BEFORE</th><th>AFTER</th></tr></thead><tbody><th>" > out.html


rot_sqlplus(){

sqlplus -s /nolog <<SQLPLUSEND
conn user/passwd@database
SET HEADING OFF;
SET FEEDBACK OFF;
SET ECHO OFF;
SET PAGESIZE 0;
select '<tr><td>'||ID|| '</td><td>' || BEFORE || '</td><td>' || BEFORE || '</td></tr>' from teste where id=$1;
quit
SQLPLUSEND

}

out=$(for id in 1 2 3 4
     do
        rot_sqlplus $id
      done)

 echo ${out} >> out.html

#finish table 
echo "</th></tbody></table>" >> out.html

cat out.html

结果到 out.html

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>BEFORE</th>
            <th>AFTER</th>
            </tr>
    </thead>
    <tbody>
        <th>
            <tr>
                <td>1</td><td>12</td><td>12</td>
            </tr>
            <tr>
                <td>2</td><td>27</td><td>27</td></tr>
            <tr>
                <td>3</td><td>45</td><td>45</td>
            </tr>
        </th>
    </tbody>
</table>

在浏览器中查看

在此处输入图像描述


推荐阅读