#!/bin/sh -u # sample answer for the CGI script exercise in CGI_script.txt # --- # a CGI script to fetch the weather for a given city code # --- # Syntax: $0 [ citycode ] # # If this script is run at a normal terminal, it behaves like a normal # shell script. If it is run without a terminal (e.g. as a CGI # script), it outputs the CGI script headers and all messages appear # on standard output. # # NOTE: Errors must go to standard output in CGI scripts, not to standard error. # We test standard input to see if it is a terminal and issue # error messages to stderr if it is and to stdout if it is not. # (CGI scripts run with standard input not a terminal.) # # This script does not contain all the proper parts of a DAT2330 script. # See the Notes file "script_style.txt" for the correct format. # Set a standard shell search path and a friendly umask. # PATH=/bin:/usr/bin ; export PATH umask 022 # If no terminal on standard input, assume CGI script and put out the # CGI script header. # if ! tty -s ; then echo "Content-Type: text/plain" echo "" fi # Input section: check the argument count to assure exactly one city code. # if [ $# -gt 1 ] ; then msg="$0: expecting one city code, found $# ($*)" if tty -s ; then echo 1>&2 "$msg" else echo "$msg" fi exit 1 fi # A missing first argument defaults to YOW (Ottawa). # if [ $# -ne 1 ] ; then citycode=YOW else citycode="$1" fi # Fetch the weather for the city code and remember the status. # echo "$0: The current '$citycode' weather is:" lynx -dump "http://weatheroffice.ec.gc.ca/forecast/city_e.html?$citycode" \ | grep -e 'Temp.:' -e 'Observed on' status=$? # Print a message if the status was not zero (on error). # if [ "$status" -ne 0 ] ; then msg="$0: Bad status lynx/grep on city '$citycode' is $status" if tty -s ; then echo 1>&2 "$msg" else echo "$msg" fi fi