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.

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

Perl script on unix Hello World Perl script for IIS on NT 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


Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / HTML_Examples / cgi.htm