----------------------- Exercise #2 for DAT2330 due February 3 ----------------------- -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: 2% of your total mark this term Due date: 11h00 (11am) Tuesday, February 3, 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 19h00 (7pm) on Thursday, February 5. 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 11h00 (11am) Tuesday, February 3, 2004. Exercise Synopsis: On the Course Linux Server, create a file with a sequence of Linux commands in it. Feed the file to the BASH shell and watch the shell execute the commands. Verify the output. Where to work: Do this work on the Course Linux Server. Do not use ACADUNIX. Exercise Preparation: A. Finish your Week 2 and Week 3 Readings (see the weekly Notes files). Any questions? See me in a lab or post questions to the Discussion news group (on the top left of the Course Home Page). B. Review the use of Unix pipes. Do *not* create temporary files for any output in this week's exercise. Use Unix pipes only. Exercise Details (on the Course Linux Server): 0. Have you done all the preparation steps? If not, go back and do them. 1. Using VI/VIM, edit a new script file named exercise02commands.sh on the Course Linux Server. The spelling of the file name must be exact, othewise it won't be marked. The spelling must be exact. Exact! At the top of the script file, create an Exterior Assignment Submission label. (Follow the instructions from the previous exercise.) New: Preface each line of your label with the BASH shell comment character "#", e.g. # Student Name: Eileen Dover ...etc... Underneath your Exterior Assignment Submission label in the script file exercise02commands.sh add the following lines, one at a time: 2. The "date" command. Add the word "date" to the bottom of the file, after the Exterior Assignment Submission label. Save the file and feed it to the BASH shell with the -u option turned on: $ bash -u exercise02commands.sh Tue Jan 27 13:16:18 EST 2004 Note how the BASH shell reads the file and ignores all lines starting with "#". The shell executes the line with the word "date" on it. The output appears on your terminal (because you did not redirect it anywhere else). Verify that the *only* output on your screen is the current date. 3. Add another command line to your script file, below the lines you have already added. The output of this new command line must be the current working directory in which the script is running: $ bash -u exercise02commands.sh Tue Jan 27 03:58:07 EST 2004 /home/alleni99 4. Add another command line to your script file, below the line you have already added. The output of this new command line must be the letters of the current date in UPPER CASE: $ bash -u exercise02commands.sh Tue Jan 27 03:36:07 EST 2004 /home/alleni99 TUE JAN 27 03:36:07 EST 2004 Hint: See the "tr" example in your lecture notes for Friday, January 23. Verify that the *only* new output on your screen is the current date with letters in UPPER CASE. 5. Add another command line to your script file, below the lines you have already added. The output of this new command line must be just the "time" portion (fourth field) of the date command: $ bash -u exercise02commands.sh Tue Jan 27 03:46:07 EST 2004 /home/alleni99 TUE JAN 27 03:46:07 EST 2004 03:46:07 Note that on February 1, there will be two blanks between the month and the digit 1. Make sure your solution to extracting the time works no matter how many blanks separate the fields. (You cannot easily use the "cut" command to do this.) 6. Add another command line to your script file, below the lines you have already added. The output of this new command line must be a character count of the total number of userids currently logged in to this machine: $ bash -u exercise02commands.sh Tue Jan 27 03:56:07 EST 2004 /home/alleni99 TUE JAN 27 03:56:07 EST 2004 03:56:07 7 Hint: Count just the lines of output of a common Unix command. Your count of lines may differ, depending on who is online when you run your script. 7. Add another command line to your script file, below the lines you have already added. The output of this new command line must be the checksum of the last five lines of the sorted Unix password file: $ bash -u exercise02commands.sh Tue Jan 27 03:57:07 EST 2004 /home/alleni99 TUE JAN 27 03:57:07 EST 2004 03:57:07 9 12137 1 Hint: You already did an exercise similar to this last week. 8. Add another command line to your script file, below the lines you have already added. Search the list of the last userids to login to this machine and find and output only the lines that contain the pattern "reboot": $ bash -u exercise02commands.sh Tue Jan 27 03:59:07 EST 2004 /home/alleni99 TUE JAN 27 03:59:07 EST 2004 03:59:07 5 12137 1 reboot system boot Tue Jan 6 03:17 (21+00:42) 2.4.21-0.13mdk reboot system boot Tue Jan 6 03:08 (00:07) 2.4.21-0.13mdk 9. Add another command line to your script file, below the lines you have already added. The command that shows the listing of last logged in users has an option to display the host name in the last column; use this option for this next command pipeline. Search the list of the last userids to login to this machine and select only the lines that do *NOT* contain the pattern "reboot". In those lines (that do not contain "reboot"), select just the last field in each line (the host name) and display the top 20 most frequent login host names prefixed with the count of times that host name appears, sorted in descending order: $ bash -u exercise02commands.sh Tue Jan 27 03:59:07 EST 2004 /home/alleni99 TUE JAN 27 03:59:07 EST 2004 03:59:07 5 12137 1 reboot system boot Tue Jan 6 03:17 (21+00:42) 2.4.21-0.13mdk reboot system boot Tue Jan 6 03:08 (00:07) 2.4.21-0.13mdk 387 pnat-atc.algonquincollege.com 17 205.211.56.77 14 cpe0007e9dacbdd-cm023459905803.cpe.net.cable.rogers.com 13 192.168.9.250 11 acadunix.algonquincollege.com 11 205.211.56.30 10 mailhost.home.idallen.ca 8 cpe0080c6f00813-cm014270110980.cpe.net.cable.rogers.com 8 aip56-30.algonquincollege.com 7 pnat-rs.algonquincollege.com 7 . 6 localhost:10.0 5 hse-ottawa-ppp234147.sympatico.ca 4 dsl-207-112-124-80.tor.primus.ca 4 dial-216-163-119-129.ssvecnet.org 4 cpe000c6ec804c6-cm00080df90e4c.cpe.net.cable.rogers.com 3 cpe0080c6e9076f-cm024420005894.cpe.net.cable.rogers.com 3 cpe0040ca64c466-cm000e5c6e48e2.cpe.net.cable.rogers.com 3 cpe002078e051c4-cm00003970fe61.cpe.net.cable.rogers.com 3 cpe0008a121e515-cm000a735ce16a.cpe.net.cable.rogers.com Your output will differ from the above, depending on who actually logs in to the machine. To get the above 20 lines of host name output requires a command pipeline containing about seven Unix commands. Build the pipeline slowly, verifying the output of each added command. Hint: See your lecture notes for Friday, January 23. 10. Documentation. Above each of the command lines you wrote in exercise02commands, insert a one-line (less than 80 characters) comment summarizing what the expected output is. Comments start with "#" in shells scripts and extend to the end of the line. Script commands without added comment lines will not be marked. 11. Create a second script file named "weather.sh". Put your Assignment Label at the top of the file using shell comment lines. You can find the current weather for Ottawa ("YOW") at this URL: http://weatheroffice.ec.gc.ca/forecast/city_e.html?YOW There is a text-mode web browser available on Linux named "lynx" that can fetch any URL and display the text results on standard output. Our use of the "lynx" command requires exactly two arguments. The first argument must be "-dump" to send output to standard output. The second argument is the URL we want to fetch, e.g. the weather for the city "YOW" (Ottawa). (Montreal is YUL; Toronto is YYZ.) Try using the "lynx" command and above arguments to fetch the current weather page onto your screen to see what you get. You will need to pipe the output of lynx into a program that shows you standard input (the output of lynx) one page at a time. Identify the line containing the current temperature. (It is above the line saying "Pressure:".) When you are confident that you can fetch the page to your screen, construct a command pipeline that will fetch, extract, and display *only* the current temperature line from the Ottawa weather page fetched by lynx. Put this command pipeline into the bottom of your new "weather.sh" shell script file. (My pipeline only needed two different commands - yours may differ.) Sample output from your working "weather.sh" script: $ bash -u weather.sh Temp.: -14°C Only the *one* line must be output when you feed weather.sh to BASH. You may wish to experiment with the weather in other cities to make sure your script works for all of them. (Change the URL in your script to request codes YVC, YPH, YRB, etc.) For more codes see: http://flightpath.morison.net/airports/canada/ Note: Not all airport codes have weather stations. Bonus: Re-format the generated output line so that it looks like this: $ bash -u weather.sh Temp -14°C (Hint: The versatile "tr" command also has options to delete characters and remove duplicate repeated characters. Although you can only do one or the other, not both, in a single invocation of tr, you can always chain together multiple tr commands using pipes.) 12. Submission. Submit both the finished exercise02commands.sh file and weather.sh file for marking as Exercise 02 on the Course Linux Server, using the following *single* datsubmit command line: $ datsubmit 02 exercise02commands.sh weather.sh This "datsubmit" program will copy the selected files to me for marking. Always submit all your files at the same time. Do not delete your copies; keep them. Verify that you submitted all your files, using this command line: $ datsubmit 02 -list Note that the digit "1" and the letter "l" (lower-case "L") are different. Do not confuse the two. You may redo this exercise and re-submit your results as many times as you like; but, you must always submit *all* your exercise files every time. The "-delete" option of datsubmit will delete the most recent submission you have made. I will mark only the most recent submission that is submitted before the final hand-in cutoff date. For Exercise 02, always use "02" as the first argument to "datsubmit". Always submit *all* the files each time you submit an exercise. A correct exercise02commands.sh is worth 80% of the mark. A correct weather.sh is worth the remaining 20%.