Вопрос

Basic Idea: I have an HTML which only has a table formed in it.

There are things like date, time, and result of some database queries. I am mailing this detail in this table manually to people till now.

I have a script which calculates all the values required in this table. But I am not sure how do I append those values directly in the html file while executing the script.

Not sure how do I accomplish this, and whether it's possible.

P.S. I am sending mail with sendmail -t have tried sending trial HTML page and it's working fine. So the real issue is how do i append the variable values from the unix script into the html file.

Это было полезно?

Решение

you probably have to provide place holders in the fixed html, then replace those with your value, using "sed". e.g. test.html:

<html>
....
<div>The date is now %%DATE%%</div>
</html>

Then your script would replace %%DATE%%:

sed "s/%%DATE%%/$(date)/"  test.html

Similarly you can replace environment varables:

export MYDATE="2013-12-27"
sed "s/%%DATE%%/$MYDATE/" test.html

Другие советы

You could turn your HTML file into a template, adding markup that won't conflict with other content and then replace the markers with shell variables, for example given an HTML:

<html>
    <head><title>{{title}}</title></head>
    <body>
        <table>
            <tr><th>Name</th><th>Date</th></tr>
            <tr><td>{{name1}}</td><td>{{date1}}</td></tr>
            <tr><td>{{name2}}</td><td>{{date2}}</td></tr>
        </table>
    </body>
</html>

you could replace the values like this:

sed -e "s/{{title}}/Recent Stats/g" -e "s/{{date1}}/$(date)/g" -e "s/{{[^}]*}}/UNKNOWN/g" | sendmail

Another solution might be to construct the HTML to send by combining a header part + data part + footer part, like this:

{ cat header.html; ./script/that/dumps/data.sh; cat footer.html; } | sendmail
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top