----------------------- Exercise #3 for DAT2330 due November 23, 2004 ----------------------- -Ian! D. Allen - idallen@idallen.ca Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Global weight: 4% of your total mark this term Due date: 10h00 (10am) Tuesday November 23, 2004. The deliverables for this exercise are to be submitted online on the Course Linux Server using the "datsubmit" method described in the exercise description, below. No paper; no email; no FTP. Late-submission date: I will accept without penalty exercises that are submitted late but before 10h00 (10am) on Thursday, November 25. After that late-submission date, the exercise is worth zero marks; but, it must still be completed and submitted successfully to earn credit in the course. Exercises submitted by the *due date* will be marked online and your marks will be sent to you by email after the late-submission date. A sample answer will be posted online after the late-submission date. This exercise is due on or before 10h00 Tuesday November 23, 2004. Exercise Synopsis: You will use "vim" to create some executable shell scripts. Where to work: Do your Unix command line work on the Course Linux Server. Do not use ACADUNIX. The files you work on will remain on the server even after you log off. Do not erase your files after submission; always keep a spare copy of your exercises. WARNING: Do not attempt this exercise on a Windows machine - the text file format is different. You must connect to and work on Unix/Linux. Note that you may connect to the Course Linux Server *from* a Windows machine (using PuTTY); however, you may not use the Windows machine itself to do your work. Use the vim editor on the Course Linux Server. Location of the course notes on the Course Linux Server: You can find a copy of all the course Notes files on the Linux Server under directory: ~idallen/public_html/teaching/dat2330/04f/notes/ You can copy files from this directory to your own account for modification or study, if you like. (To avoid plagiarism charges, you must credit any material that you copy and submit unchanged.) Exercise Preparation: A. Know where to find an online copy of all the course Notes on the Course Linux Sever. (See above and in course_linux_server.txt) Do not use the ACADUNIX computer for this exercise. B. Do *not* create temporary files if using a Unix pipe would work. -------------------------------------------------------- Exercise Details (to be done on the Course Linux Server) -------------------------------------------------------- Have you done all the preparation steps? If not, go back and do them. Using VI/VIM, edit and create new executable script files on the Course Linux Server. The spelling of each script file name must be exact, othewise it won't be marked. The spelling must be exact. Exact! -------------------- For all script files -------------------- Make sure each executable script file you write starts with the nine necessary parts of a DAT2330 executable shell script, as given by the script_style.txt file under Notes. Make sure the file is executable (at least by you, the owner) and does *not* have group or other write permissions turned on. Follow the format given in the script_style.txt file under Notes. Print error messages and exit the script on bad input. --------------------------- Script name: myweather.sh --------------------------- Marks: 4 of 4% Begin the myweather.sh script as given above in "For all script files". Write an executable shell script that takes two optional command line arguments: $0 [ city_code [ weather_item ] ] * Input section: If standard input is a terminal, prompt and read from standard input any missing command line arguments; otherwise, (if standard input is not a terminal and some arguments are missing) a missing city code should default to "YOW" and a missing weather item should default to "temperature". If you use default values, output a line telling the user which default values are being used. Do not duplicate code in this section! Less code is better code! * Validation section: The city code should be a three-letter airport code. Make sure this string entered by the user contains exactly three characters; print an error message and exit the script with exit status 2 if this is not true. The weather item entered by the user should be a word from this list: temperature pressure visibility humidity Print an error message and exit the script with status 3 if the weather item entered by the user is not one of the above four words. To save duplication, you may want to combine this test with the pattern selection code needed in the processing section below. * Processing section: The script should make the three-character city code upper-case before using it, and for display in any output messages. Fetch the formatted weather web page for the given city code. (Do not fetch the raw HTML page.) You can fetch the formatted page into a temporary file under /tmp/ or into a shell variable; you choose. If you use a temporary file, remember to remove it when you are done. If the fetched formatted weather web page contains the word "Error", tell the user that the weather for that city code is not available and exit the script with status 4. Based on which of the four weather items the user gives, choose a pattern that will extract from the fetched weather web page the line containing the weather value desired. (i.e. If the user enters "temperature" you need to look for the pattern "Temp.:" in the web page to get the line that contains the temperature value.) Do not duplicate code in this section! Less code is better code! Extract just the desired value from the line; remove all the other words (e.g. extract just "84%" or "-1°C"). Also use a pattern to extract the line containing the actual city name (e.g. Vancouver) from the same weather web page. Extract just the city name from that line (e.g. extract just "Vancouver"). You will need to output this city name later. * Output section: Display the weather item, the city code, and the results in this format: $ ./myweather.sh yvr temperature The temperature in YVR - Vancouver - is 8°C $ ./myweather.sh yow pressure The pressure in YOW - Ottawa North (Kanata - Orleans) - is 101.9 $ ./myweather.sh yul visibility The visibility in YUL - Montréal - is 16km $ ./myweather.sh yqb humidity The humidity in YQB - Québec - is 78% $ ./myweather.sh x humidity ...you print an error here about the wrong length city code... $ ./myweather.sh yqb junk ...you print an error here about the unknown weather item... $ ./myweather.sh xxx temperature ...you print an error here about the weather not being available... * Testing Make sure the script prompts for missing arguments if standard input is a terminal: $ ./myweather.sh ...you issue a prompt for the city code and weather item here... yqb visibility <=== the user types this input The visibility in YQB - Québec - is 24km Make sure the script uses default values for missing arguments, if any arguments are missing and if standard input is not a terminal: $ ./myweather.sh