HTML Examples - Unix CGI
cgi files are programs which are executed on a unix server.
In general, everything sent to the standard output is returned
as an HTML page.
Thus, it is possible to have a program generate the page that
will be displayed.
cgi files are not limited to any particular programming language.
Shell scripts, perl scripts, compiled executables,
and any other executables are allowed.
When writing a shell script (an interpreted text file),
the first line tells unix which shell to execute.
All the following shell commands send data
to the standard output.
echo
| Send a string to the standard output.
|
grep | Search a file and send matching strings to the
standard output.
|
egrep | Similar to grep, but uses regular expressions.
Can match any of several strings.
A single period is a wildcard.
Use backslash-period (\.) to match a period.
|
awk | Simple string processing. Parses string.
Has substring command.
|
cut | Column and field based string processing.
|
When copying a shell script (text file) from a PC to a unix machine,
do NOT use binary mode FTP;
use ASCII mode instead.
The problem has to do with the end-of-line (EOL) characters.
In text (ASCII) mode, the 2-character EOL sequence (CR LF) is
converted to a single EOL character.
When a file is transferred in binary mode,
this conversion is not made and
the shell script will not execute.
Examples
One poorly documented trick is that there must
be a blank line before the first line of real html.
If this is missing, IIS returns a "misformed header" error.
(It only took 2 days to re-discover this secret.)
Unix shell script
#!/bin/sh
echo Content-type: text/html
echo
echo \\\404 Errors\\
Perl script on unix
#!/usr/bin/perl
print "Content-type: text/html\n\n";
Hello World Perl script for IIS on NT
#!c:\perl\bin\perl -w
print "HTTP/1.0 200 OK\n";
print "Content-Type: text/html\n\n";
print "\n";
print "\n";
print "Hello World\n";
print "\n";
print "\n";
print "Hello World
\n";
print "\n";
print "\n";
Notice that the perl path is different for unix and NT.
On unix, use which perl to locate the perl interpreter.
The Java command println is not supported, use
print "some text\n";
instead.
Debug the cgi files at a command prompt in a telnet session.
An example
Survey Form,
Results Form,
and perl source
based on information from
CGI Programming 101.
References
- Matt's Script Archive
Contains a super collection of CGI scripts,
including Guestbook and FormMail.
- Dave Cross
argues that the scripts in Matt's Script Archive
should not be used, mostly due
to poor programming practices and security issues.
Instead,
he provides various drop-in replacements,
including Guestbook and FormMail.
- CGIndex contains an organized list of links.
The site is obviously database driven.
-
CGI Programming 101, Perl for the World Wide Web,
by Jacqueline D. Hamilton, (also available in book form)
provides explained examples including
Form to email
Form to flat file
Flat file to html page
-
Perl manual
Author: Robert Clemenzi -
clemenzi@cpcug.org