Unix CGI Scripts
by Philip Greenspun for the Web Tools Review
CGI stands for Common Gateway Interface. It is an abstraction barrier
between the server program on your Unix box and any scripts that you
may write. This lets you run the same script with NCSA HTTPD or
Netcape's Commerce Server or the CERN server or whatever.
You should probably read some of the interface documentation
before writing any of your own scripts, but the ten-second explanation
is the following:
- the server stuffs a bunch of information into Unix "environment
variables", e.g., the name of the host that made the request
- form variable values get to the script via an environment
variable (if a GET) or via standard-in (if a POST).
- the server binds standard-out effectively to "the client's screen"
so that the CGI script thinks it is writing straight to the user.
- the first thing the CGI script must write is a
content-type header that tells the client what sort of data
to expect (i.e., HTML, plain text, a GIF, a JPEG, ...).
Example in Perl
Perl is one of the nastiest computer languages yet developed but it is
universal and very popular for writing CGI scripts. If you ask around
on USENET or surf the Web, you will probably be able to find a perl
script that does almost what you want and then modify it.
# the first line in a Unix shell script says where to find the
# interpreter. If you don't know where perl lives on your system, type
# "which perl" at any shell and put that after the #!
print "Content-type: text/html\n\n";
# now we have printed a header (plus two newlines) indicating that the
# document will be HTML so whatever else we write to standard output will
# show up on the page
print "<h3>Hello World</h3>";
More Perl examples
Return to Web Tools Review home
Add a comment | Add a link