How to Use a Unix Shell Script to Create an HTML Web Page
Unix shell scripts can be used to create web pages that display the output of basic Unix commands or complex programs. This type of script is called a CGI (Common Gateway Interface) script and is used to interact with the actual web server and produce dynamic content on the web page. Unix shell scripts can only be used on Linux or Unix based web servers. The following example shows how to use a bash script to show the output of the commands "uname -a" and "uptime" on a web page.
Instructions
-
-
1
Open a text editor such as vi, nano, or gedit.
-
2
Type the line "#!/bin/bash" to start the shell script.
-
-
3
Type the line "echo "Content-type: text/html"" to begin the html portion of the script.
-
4
Type the line "echo" to keep the script from returning a malformed header error when it is run on the web server.
-
5
Type the next three lines to output the html header section and begin the body section of the page:
echo "<html>"
echo "<head><title>Test Script</title></head>"
echo "<body>" -
6
Type the next six lines to run the "uname -a" and "uptime" commands and format the output for the web page:
echo "Output of uname -a :<pre>"
uname -a
echo "</pre><br />"
echo "Output of uptime: <pre>"
uptime
echo "</pre><br/>" -
7
Type the next two lines to complete the webpage:
echo "</body>"
echo "</html>" -
8
Save the file with the ".cgi" file extension. The entire script will look like:
#!/bin/bash
echo "Content-type: text/html"
echo
echo "<html>"
echo "<head><title>Test Script</title></head>"
echo "<body>"
echo "Output of uname -a :<pre>"
uname -a
echo "</pre><br />"
echo "Output of uptime: <pre>"
uptime
echo "</pre><br/>"
echo "</body>"
echo "</html>" -
9
Place the file in the directory that your web server uses for CGI scripts. This directory is often called "cgi" or "cgi-bin."
-
10
Test the script by typing "http://www.example.com/cgi-bin/file.cgi" in a web browser. Replace the web address with the address and file name for your file.
-
1
Tips & Warnings
You can use this type of script to format the output of any Unix command or script for display on a web page.