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.

Things You'll Need

  • Linux or Unix based web server
Show More

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.

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.

Related Searches:

References

Comments

You May Also Like

  • Unix Screen Command Tutorial

    The Unix command line provides access an extensive list of software tools designed to maximize command line efficiency. The Screen command features...

  • How to Write in Shell Script

    Learning the command line in Linux is always the first step for a new Linux user. If you want to start doing...

  • How to Write a Script in Linux

    How to Write in Shell Script. Learning the command line in Linux is always the first step for a new Linux user....

  • How to Create a File in Shell Script

    A shell script is a text file on the Linux and Unix operating systems that contains a sequence of commands that are...

  • Unix Shell Scripting Tutorial

    A shell is a software interface with which the user interacts. A Unix shell is a shell built upon the Unix operating...

  • How Are Shells Created?

    Along the beaches of the world, many people spend time combing the shoreline in search of seashells. These shells are typically the...

  • How to Embed an FTP With Shell Script

    If you want to automatically connect to a file transfer protocol (FTP) server when you perform a task on your Unix or...

  • How to Make a Flood Script

    Floods, or denial-of-service attacks, attempt to overwhelm computer resources and make them unavailable for users. Usually, attacks bombard target resources with ...

  • How to Write a Bash Script in UNIX

    Bash is one of the shell environments in Unix and Linux. A Bash script is a way to write small programs (i.e.,...

  • How to Make Shells for Cannoli

    Cannoli are an Italian dessert that originated in Sicily. Deep fried tubes are filled with either a custard cream or ricotta cheese....

  • How to Run a Script on a Web Page

    Using a scripting language like PHP or JavaScript can add a great deal of functionality to your website. Scripts are what make...

  • How to Create a Web Page

    With the tools now available on the Internet, it is easy to create a web page of your own. Whether you want...

  • How to Write a Shell Script to Delete Files

    Removing files using the command line is a simple task with the right arguments. However, if you have a lot of files...

  • How to Shell Shrimp

    It's easier to eat shrimp that have been shelled prior to cooking, but the shells do add flavor to the dish. You...

  • Shell Casing Identification

    The science of ballistics is perhaps one of the most important in contemporary law enforcement. It may be the deciding element in...

  • How to Write a Shell Script in Unix

    Under UNIX, and UNIX-like operating systems such as Linux and Mac OS X, shell scripting makes the automation of tasks much easier....

  • How to Create a Login Web Page

    A web page requesting a login appears when your web browser hits a directory on a web server that is password protected....

Related Ads

Featured