============================= Building a CGI Script on Unix ============================= -IAN! idallen@ncf.ca A CGI script is a dynamically-generated web page. The content of the page is produced not from a static text file, but rather from the output of programs running on the web server when you download the page. The content of the web page changes depending on the output of the program. You can build a dynamic web page using Unix commands, without knowing any Perl or other CGI scripting language. Here's how. 1) Create a public_html directory in your HOME directory on the Course Linux Server. Make sure it has read and execute permissions for everyone. 2) Create a file named public_html/hello.txt and put some text into it. Make sure the file is readable by everyone. 3) Enter a URL in this format into your web browser: http://dat2330.idallen.com:2331/~abcd0001/ Replace abcd0001 with your Unix userid. Make sure you put all the punctuation characters in the right places. If you get a permission denied error, fix the permissions on the public_html directory. Make sure the public_html directory has both "r" *and* "x" permissions for everyone. Make sure your userid has a tilde ('~') in front of it. You should now see the name of your hello.txt file. Click on it to make the text file display in your browser. If you get a permission denied error, fix the permissions to allow others to read the file. (The file needs "other" read permissions to allow the web server to read the file and serve it to your browser.) 4) Create a cgi-bin directory inside the public_html directory. Make sure it has execute permissions for everyone. Once again enter the URL: http://dat2330.idallen.com:2331/~abcd0001/ and make sure you can see the new cgi-bin directory name as well as your hello.txt file. Do not click on the cgi-bin directory in your browser; the web server will not let you see files there. 5) Put these next lines of text into the file public_html/cgi-bin/date.cgi. The lines must start at the left margin. No indenting! No blank line! #!/bin/sh -u # This is a CGI script. PATH=/bin:/usr/bin ; export PATH umask 022 echo "Content-Type: text/plain" echo "" date Display and word-count the file to verify that it looks EXACTLY as above. $ wc public_html/cgi-bin/date.cgi 7 18 118 public_html/cgi-bin/date.cgi You can also checksum the file to make sure it is the same as mine: $ sum public_html/cgi-bin/date.cgi 59997 1 6) Turn on read and execute permissions (for you, group, and other) on the new date.cgi file. 7) Test the executable file at the shell prompt first: $ cd public_html/cgi-bin $ ./date.cgi Content-Type: text/plain Tue Nov 9 02:18:15 EST 2004 Verify that the first line of output is "Content-Type: text/plain". The second line must be blank. The last line should be the output of "date". Fix any errors you find. The output should be three lines and exactly 55 characters: $ ./date.cgi | wc 3 8 55 8) Call up your executable file using your web browser: http://dat2330.idallen.com:2331/~abcd0001/cgi-bin/date.cgi You should see the current date. Refresh or reload the page, and you will see the seconds change on each refresh as the script executes. If you have errors, go back and re-do the previous steps. 9) Copy the date.cgi script to weather.cgi in the same directory. Modify the end of weather.cgi to include the current weather by adding these lines to the script after the "date" command line: echo "$0: The current YOW (Ottawa) weather is:" lynx -dump 'http://weatheroffice.ec.gc.ca/forecast/city_e.html?YOW' \ | grep -e 'Temp.:' -e 'Observed on' Test the weather.cgi script from the Unix command line, as you did when testing the date.cgi script, then execute your new weather.cgi script in your browser: http://dat2330.idallen.com:2331/~abcd0001/cgi-bin/weather.cgi Your browser window should display text similar to this: Tue Nov 9 02:27:01 EST 2004 weather.cgi: The current YOW (Ottawa) weather is: Observed on: 9 Nov. 2004 at 2:00 AM EST Temp.: -6°C If it doesn't work, go back and test your CGI script from the Unix command line. 10) Experiment with any other Unix commands you know. You can change the commands at the end of the script to do anything you want. You can make web pages that do these things: - Send a line of text to your logged-in userid every time someone accesses the page. (using "write") - Check the list of users currently on the machine. (using "who") - Show the most recent 10 userids to log in. (using "last") - etc. You must not change the first two lines of output of your script; the first line must always be "Content-Type: text/plain" and the second line must always be empty. You can change any of the lines that follow those first two lines. Always test your CGI script from the Unix shell command line before trying it using the URL in the web browser. The web browser will not show you the standard error messages from any commands; only the Unix command line will show those problems. ------------------------------- Argument Passing to CGI scripts ------------------------------- You can pass one single command-line argument to your CGI script by appending a question mark and word to the end of your URL, e.g.: http://dat2330.idallen.com:2331/~abcd0001/cgi-bin/weather.cgi?ABC The text "ABC" given by the user at the end of the URL is available inside your CGI script as argument $1 or in variable $QUERY_STRING (an environment variable set by the web server). You may use either $1 or $QUERY_STRING inside your script. If you print the environment variables at the end of your CGI script (using the printenv command), you will see many other variables set by the web server when it executes your script. -------- Exercise -------- Write a CGI script that shows the weather for the city given as the word at the end of the URL, e.g. http://dat2330.idallen.com:2331/~abcd0001/cgi-bin/weather.cgi?YYZ If no city is given (no argument; or, an empty $QUERY_STRING), give the weather for Ottawa (YOW). If the return status of the lynx/grep pipeline is not zero, display that status on standard output at the end of the script: $ ./weather.cgi ./weather.cgi: The current weather for YOW is: Observed on: 9 Nov. 2004 at 2:00 AM EST Temp.: -6°C $ ./weather.cgi XXX ./weather.cgi: The current weather for XXX is: ./weather.cgi: lynx/grep had error status 1 for city code 'XXX' Now try it in your browser: http://dat2330.idallen.com:2331/~abcd0001/cgi-bin/weather.cgi?YOW http://dat2330.idallen.com:2331/~abcd0001/cgi-bin/weather.cgi?YUL http://dat2330.idallen.com:2331/~abcd0001/cgi-bin/weather.cgi?YYZ http://dat2330.idallen.com:2331/~abcd0001/cgi-bin/weather.cgi http://dat2330.idallen.com:2331/~abcd0001/cgi-bin/weather.cgi?XXX Make sure you can see the lynx/grep status message when you give a bad city code. Note: For CGI scripts, error messages must be written to standard output, not to standard error. (Non-CGI shell scripts must write error messages to standard error, not to standard output.) Text sent to standard error from a CGI script does not appear in the web page.