----------------------- Exercise #1 for DAT2330 due October 12, 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: 3% of your total mark this term Due date: 10h00 (10am) Tuesday October 12, 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 Friday, October 15. 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 October 12, 2004. Exercise Synopsis: You will use "vim" to create a text file, read in another file, perform some simple edits, and save the result. You will create a second 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 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) B. Complete the VIM tutorial and all the readings. Any questions? See me in a lab or post questions to the Discussion news group (on the top left of the Course Home Page). C. 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. Part I - exercise01text.txt ------ 1. Using VI/VIM, edit a new file named exercise01text.txt 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 file, create an Exterior Assignment Submission label following the example you will find under the "Assignment Standards" button on my teaching home page (teaching.idallen.com). For full marks, follow the directions for the label exactly. The label has exactly 7 lines, plus an optional Comments line. The spelling of the label fields on the seven lines must be exactly as shown. The spelling must be exact. Exact! Do the following in your exercise01text.txt file on the Course Linux Server, underneath your Exterior Assignment Submission label: 2. From the file opt_why_shell.txt in the Notes directory extract just the paragraph that starts with the words "Back in" and include it in the bottom of your exercise submission file. (Hints: you could read the entire opt_why_shell.txt file into your exercise file and edit away the unnecessary lines, or, you could copy the opt_why_shell.txt file to a temporary file and edit away the unnecessary lines, then read the edited version into your exercise file; or, you could use "head" and "tail" and pipes and redirection to extract the lines into a temporary file and read the temporary file into your exercise file.) Do not change the spacing or formatting of this paragraph. Note the start and end line numbers of this single paragraph and use a single VIM command to globally change all "and" to "***" in this paragraph only. (Hint: VIM tutorial Lesson 4.4.) Part II - exercise01commands.sh ------- 3. Using VI/VIM, edit a new script file named exercise01commands.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 exercise01commands.sh add the following Unix command lines, one at a time: 4. 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 exercise01commands.sh Tue Oct 5 09:25:50 EDT 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. 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 the current working directory in which the script is running: $ bash -u exercise01commands.sh Tue Oct 5 09:25:50 EDT 2004 /home/alleni99 6. 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 current list of online users, except that all UPPER CASE characters must be made into lower-case characters: $ bash -u exercise01commands.sh Tue Oct 5 09:25:50 EDT 2004 /home/alleni99 idallen pts/0 oct 2 11:38 (mailhost.home.idallen.ca) Hint: See the "tr" examples in your lecture notes. 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 just the current size in bytes of the Unix password file: $ bash -u exercise01commands.sh Tue Oct 5 09:25:50 EDT 2004 /home/alleni99 idallen pts/0 oct 2 11:38 (mailhost.home.idallen.ca) 2973 Hint: Build a Unix pipeline. First generate a line of output showing the size of the file. Send that output into another Unix command that extracts just the field containing the size in bytes. 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" in the month of September: $ bash -u exercise01commands.sh Tue Oct 5 09:25:50 EDT 2004 /home/alleni99 idallen pts/0 oct 2 11:38 (mailhost.home.idallen.ca) 2973 reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:47 (27+18:55) reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:40 (00:05) reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:26 (00:11) Hint: Build a Unix pipeline. First generate output showing the list of all users who last logged in. Send that output into a command that shows only the lines containing the reboot pattern. Send that output into a program that shows only the lines containing the pattern for the month of September. 9. 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 count of the total number of userids who have ever logged in to this machine on a Tuesday (at least as far back as the login records show): $ bash -u exercise01commands.sh Tue Oct 5 09:25:50 EDT 2004 /home/alleni99 idallen pts/0 oct 2 11:38 (mailhost.home.idallen.ca) 2973 reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:47 (27+18:55) reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:40 (00:05) reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:26 (00:11) 568 Hint: Build a Unix pipeline. First generate output showing the list of all users who last logged in. Send that output into a command that shows only the weekday field in each line. Send that output into a command that finds lines that match the pattern for Tuesday. Send that output into a program that counts lines. (Note: Show only one single number in your output!) 10. 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 three userids that have done the most logins to this machine, prefixed with the count of the number of times they logged in, sorted in descending order by login frequency: $ bash -u exercise01commands.sh Tue Oct 5 09:25:50 EDT 2004 /home/alleni99 idallen pts/0 oct 2 11:38 (mailhost.home.idallen.ca) 2973 reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:47 (27+18:55) reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:40 (00:05) reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:26 (00:11) 568 218 davi0327 128 idallen 116 wang0044 Hint: See your lecture notes for Monday, October 4. 11. 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 5 most frequent login host names prefixed with the count of times that host name appears, sorted in ascending order by count: $ bash -u exercise01commands.sh Tue Oct 5 09:25:50 EDT 2004 /home/alleni99 2973 reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:47 (27+18:55) reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:40 (00:05) reboot system boot 2.4.21-0.13mdk Tue Sep 7 08:26 (00:11) 568 218 davi0327 128 idallen 116 wang0044 78 mailhost.home.idallen.ca 106 pnat-rs.algonquincollege.com 108 aip65-77.algonquincollege.com 475 aip65-50.algonquincollege.com 1154 pnat-atc.algonquincollege.com Your output will differ from the above, depending on who actually logs in to the machine. To get the above lines of host name output requires a command pipeline containing about seven Unix commands. Build the pipeline one piece at a time, verifying the output of each command before you pipe it into the next command. Hint: See your lecture notes for Monday, October 4. 12. Documentation. Above each of the command lines you wrote in exercise01commands.sh, insert a one-line (less than 80 characters) comment summarizing briefly what the expected output is for the pipeline that follows. Comments start with "#" in shells scripts and extend to the end of the line. Script commands without added comment lines will not be marked. 13. Submission. Submit both the finished exercise01text.txt and exercise01commands.sh files for marking as Exercise 01 on the Course Linux Server, using the following *single* datsubmit command line: $ datsubmit 01 exercise01text.txt exercise01commands.sh This "datsubmit" program will copy the two 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 01 -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 01, always use "01" as the first argument to "datsubmit". Always submit *all* the files each time you submit an exercise. A correct exercise01text.txt is worth 10% of the mark. A correct exercise01commands.sh is worth 90% of the mark. P.S. Did you spell all the label fields and file names correctly?