#!/bin/sh -u # A CGI weather script to show the current temperature in Ottawa. #------------------------------------------------------------------ # Syntax: $0 (no command line arguments used) #------------------------------------------------------------------ # Purpose: NET2003 Data Mining using Shell Scripts # Display an image. # Display some contact information. # Display the current weather for Ottawa. # - fetch and format just the Temperature from the Ottawa weather page # - use shell commands to do the Data Mining # HTML reference: http://www.w3schools.com/tags/default.asp # CSS Styles Reference: http://www.w3schools.com/css/css_reference.asp # # Comments starting with #I! are instructor comments to students. # They would not appear in a normal shell script. #------------------------------------------------------------------ # License: GNU Public License # -Ian! D. Allen - idallen@idallen.ca - www.idallen.com ############################################################################# # CGI script set-up. # This will be an HTML document. Set the content type and charset. # If input is not a terminal, assume we are a CGI script and generate the # two HTTP header lines needed. Redirect stderr onto stdout (into browser): # content='text/html; charset=iso-8859-1' if [ ! -t 0 ] ; then echo "Content-Type: $content" echo "" exec 2>&1 fi ############################################################################# # Shell script set-up. # Use standard search path, friendly umask, ASCII collating and sorting. # Set the language and character set to be ASCII/C standard. # PATH=/bin:/usr/bin ; export PATH LC_COLLATE=C ; export LC_COLLATE LANG=C ; export LANG umask 022 ############################################################################# # HTML document set-up. # Output the content type in the HTML header as well as above. # Wrap long lines at 40em width. Set a background colour. #I! Note the use of single quotes inside double. # echo "" echo "" echo "Ian!s Face and Weather Page" echo "" echo "" echo "" echo "

Ian!s Face and Weather Page

" # Let the image float to the right of the following text. # Pad the image on the left (but not top, right, or bottom). #I! Note the use of double quotes inside single. # echo '' # Give some contact information. # echo "

Contact Information

" echo "

" echo "You can contact me at:" echo "" echo "idallen@idallen.ca" echo "" echo "

" echo "

" echo "My home page is here:" echo "" echo "idallen.com" echo "" echo "

" ############################################################################# # Data Mining Section. # Fetch the whole formatted text version of the weather into a variable. # The formatted page is easier to parse than the raw URL. #I! You can use "wget -O" to fetch the unformatted HTML version. #I! Note the need to quote the URL because of the shell GLOB metacharacter. #I! Always double-quote all your variables! # url='http://text.weatheroffice.gc.ca/forecast/city_e.html?on-118' page=$( lynx -display-charset=iso-8859-1 -dump "$url" ) # Error checking the web page: # Check for the string "Temperature:" in the returned web page. # Print the error message in red if we can't find it. #I! The IF statement tests the return status from grep (the last command). #I! Note the use of leading ! to invert the return status of the comand line. #I! The -q option to grep suppresses the output of grep. #I! All I care about is the return status, not the output. # if ! echo "$page" | grep -q "Temperature:" ; then # We didn't find any Temperature for some reason. Site down? echo "

" echo "Error Fetching Weather Page for Ottawa via $url" echo "

" # Output what we found with the HTML escaped to square brackets #I! The "tr" translate command is very useful in data mining. echo "
"
        echo    "$page" | tr '<>' '[]'
        echo "

" # Explain that the temperature is not available obsline='for Ottawa' templine='not available' else # We found "Temperature:" - it must be a valid weather page. # Use grep to extract the line containing "Observed". # Use grep to extract the line following "Temperature". #I! Send the saved page contents through grep twice to fetch two lines. #I! Command substitution captures the line output by grep. #I! The -A and -B options to grep can be helpful with data mining. # obsline=$( echo "$page" | grep 'Observed ' | head -1 ) templine=$( echo "$page" | grep -A 1 -e 'Temperature:' \ | head -2 | tail -1 ) fi ############################################################################# # Output the found data with a title and italic and bold emphasis: #I! This could all be one single echo statement; but, I liked this better: # echo "

An Interesting Web Fact

" echo "

" echo "The temperature" echo "$obsline" echo "is" echo "$templine" echo "

" ############################################################################# # HTML document closing. Script exit. # echo "" echo "" exit 0