|
This article shows how to use a bash script to build a web page programmatically. You can build simple, or very complex web pages with bash scripts. Building a web page with a bash scripts involves the use of what are called "here documents". This trick will send whatever is between a couple of file start, and end descriptors to a file. A short here document looks like the following code snippet. cat > $DirectoryName/WebPage.html << EOF <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"> <title>Web Page Built by Bash Script</title> <meta content="C Ray Parrish" name="author"> <meta content="A web page built by a bash script which includes the output of bash commands." name="description"> </head> <body> <p>Content of body goes here.</p> </body></html> EOF The previous here document has been redirected to the file $DirectoryName/WebPage.html, and this call will overwrite any existing file with that name. To append to a page in progress from a later here document be sure to add two > symbols to the redirection portion of the command instead of one. Everything between the two "EOF" markers will be written to the new web page. You do not need to use "EOF" for start, and end points. Any other repeated start, and end pair will work, as in "FILE" to "FILE", or "WRITE", and "WRITE" in place of "EOF" in both places. Here is the source code for a simple example bash script that writes a web page based on the output of a couple of bash commands into variables.
#!/usr/bin/env bash
#
# USAGE: ~/FOLDER/PATH/BuildWebPage.sh
# This script expects no command line parameters.
#
# This function builds a web page with some variable input from bash commands.
function BuildWebPage {
# Do some set up
DirectoryName=`dirname $0`
cd $DirectoryName
# Gather the output of some commands into variables.
UserName=`echo $USER`
MemoryUsage=`free`
# Build the web page with the variables used to add output to the web page.
# This involves the use of a "here document".
cat > $DirectoryName/WebPage.html << EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title>Web Page Built by Bash Script</title>
<meta content="C Ray Parrish" name="author">
<meta content="A web page built by a bash script which includes the output of bash commands." name="description">
</head>
<body>
<table style="text-align: left; width: 100%;" border="2" cellpadding="2"
cellspacing="2">
<tbody>
<tr>
<td style="vertical-align: top; background-color: rgb(153, 255, 255);">
User name of logged in user
</td>
<td style="vertical-align: top; background-color: rgb(153, 255, 255);">
$UserName
</td>
</tr>
<tr>
</td>
<td colspan=2 style="vertical-align: top; background-color: rgb(153, 255, 255);">
Memory Usage from free command below.
</td>
</tr>
<tr>
</td>
<td colspan=2 style="vertical-align: top; background-color: rgb(153, 255, 255);">
<pre>
$MemoryUsage
</pre>
</td>
</tr>
</tbody>
</table>
<p>You can of course add as much to this here document as you wish to build your web page.</p>
</body>
</html>
EOF
# Load the resulting page into the default browser on this system.
# get the command for the users default browser on this system.
BROWSER=`gconftool-2 --get '/desktop/gnome/url-handlers/http/command' | cut -f1 -d' ' `
# call that browser with the web page url of the page we just built.
${BROWSER} $DirectoryName/WebPage.html &
}
# Main Program starts here.
BuildWebPage
exit
In the example script above, the $UserName, and $MemoryUsage within the body of the here document gets replaced with the values of those two variables. To see the output of the script you will need to make sure it has execute permissions, then run it in any folder under your user folder. After the scripts runs, it will load the newly created web page into your web browser. You can make as simple, or as complex of a web page as you desire using the above technique. To see more examples of this kind of here document redirection examine the script SystemReport.sh as found on the page - System Report for Ubuntu which makes extensive use of this method. You can also use command substitution to write to the here document instead of just using a variable. The following is an example of this technique. Look for the call to the WriteTableBody function within the here document. Note that it must be in `` backquotes in order for command substitution to work.
#!/usr/bin/env bash
#
# The following function writes it's output to a variable assignment, or to the here document
# directly.
function WriteTableBody {
# Output some data in HTML tags.
echo "<tr><td>Apples</td><td>50 cents a pound</td></tr>"
echo "<tr><td>Oranges</td><td>60 cents a pound</td></tr>"
echo "<tr><td>Coffee</td><td>5 dollars</td></tr>"
echo "<tr><td>Milk</td><td>$1.99 a gallon</td></tr>"
}
# Main program begins here.
cat > $DirectoryName/WebPage.html << EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title>Web Page Built by Bash Script</title>
<meta content="C Ray Parrish" name="author">
<meta content="A web page built by a bash script which includes the output of bash commands." name="description">
</head>
<body>
<table style="text-align: left; width: 100%;" border="2" cellpadding="2"
cellspacing="2">
<tbody>
`WriteTableBody`
</tbody>
</table>
</body>
</html>
EOF
exit
For more examples of writing to HTML files with a bash script have a look at the Linux dpkg Sotware Report script which uses several techniques to write to HTML files, and is a much more complex example of what can be done. Like this page? Link to it from your own website; just copy/paste this HTML:Not finding what you're looking for? Try the search box below. Custom Search
| |
| Last modified on: |