Sometimes scripts can be quite simple and still have useful results. For example, while I was developing some of the scripts for this chapter, Apple released its Safari web browser. My immediate question was, "How does Safari identify itself within the HTTP_USER_AGENT string?"
Finding the answer is quite a simple task for a CGI script, a script that can be written in the shell.
#!/bin/sh # showCGIenv - Displays the CGI runtime environment, as given to any # CGI script on this system. echo "Content-type: text/html" echo "" # Now the real information echo "<html><body bgcolor=\"white\"><h2>CGI Runtime Environment</h2>" echo "<pre>" env printenv echo "</pre>" echo "<h3>Input stream is:</h3>" echo "<pre>" cat - echo "(end of input stream)</pre></body></html>" exit 0
When a query comes from a web client to a web server, the query sequence includes a number of environment variables that the web server (Apache, in this instance) hands to the script or program specified (the so-called Common Gateway Interface). This script displays this data by using the shell env command, with the rest of the script being necessary wrapper information to have the results fed back through the web server to the remote browser.
To run this code, you need to have the script executable and located on your web server. (See the earlier section "Running the Scripts in This Chapter" for more details.) Then simply request the saved .cgi file within a web browser.