Friday, 18 November 2016

Embedding some HTML in a shell script


Shell scripts are awesome & another awesome trick I came across recently is the ability to wrap our shell commands around some basic html tags & redirect the content to a html file. Then we can view the output of our shell commands in a web page. This can be pretty useful when we need to share some command outputs as reports within other internal teams or to upper management. A web page output may be more easy to read as compared to a text file.

Here is the script I wrote:

[root@centdb DB]# cat web.sh
#!/bin/bash

echo "<html>
<body text="blue">

<h1> $(hostname) </h1>
<h2> script ran at $(date) </h2>

<font face="verdana" size="4">

<pre>
<font color="red"> system uptime is</font>
$(uptime)
</pre>

<pre>
<font color="red"> system disk utilization is</font>
$(iostat -xt)
</pre>

<pre>
<font color="red">File system utilization is</font>
$(df -h)
</pre>

</font>

</body>
</html>" > web.html


It's some basic stuff. I echoed the content of the entire script body to the output html file web.html. I used basic commands like uptime, iostat & df to illustrate the usage. You can do more complex commands or even use functions if you'd like. 
As far as the html part is concerned, that too is basic stuff with some usage of the font tag to add some color here n there.

When you open the resultant web.html file in a browser, it looks like this:



No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...