----------------------- Exercise #3 for NET2003 due January 24, 2005 ----------------------- -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: Before the end of your Lab period on Monday, January 24. 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 21h00 (9pm) on Monday, January 24. After that late-submission date, the exercise is worth zero marks. 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. This exercise is due before the end of your Lab period on January 24. Exercise Synopsis: Marks: 2% Introduction to shell scripts: Create an executable shell script that creates a directory structure and manipulates files inside of it. Where to work: Do your Unix command line work on the Course Linux Server. 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/net2003/05w/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: Notes: see notes and readings in week03notes.txt --------------------------------------------- Exercise Details (on the Course Linux Server) --------------------------------------------- 0. Have you done all the preparation steps? If not, go back and do them. You may find it useful to create separate directories in your account to store the files for each exercise. 1. Fetch a copy of the argv.sh.txt file from the course notes directory and rename it to be: myscript.sh Make the file executable: chmod +x myscript.sh 2. At the top of the myscript.sh file, fix the existing Assignment Submission label comment to be your own label. Make sure the lines of the label start with the shell comment character '#'. 3. Under "Syntax", replace [args...] with (no arguments) 4. Under "Purpose", replace the paragraph with a short description of what this shell script does (see below). 5. Delete everything after the line "umask 022" and replace it with executable command lines that will do the following to create the directory structure described below. You may want to open up another window and work with the shell interactively to test the commands you come up with. Copy each working command into the shell script as you debug it in the interactive window. Test your script periodically (after each edit) by saving it and running it from the command line: $ ./myscript.sh Make sure there are no error messages. NOTE: Do not use any "change directory" commands in your script unless the specifications below explicitly ask for them. a) Use a single command to completely (recursively) remove the directory named "test ex3 dir" and all its contents. This directory name has blanks but no quotes in it. There is an option that tells the command to ignore nonexistent files, so you can remove this directory even if it isn't there without getting an error message (RTFM). b) Create a new (empty) directory named "test ex3 dir". The name has blanks but no quotes in it. We will call this the "top" directory. Make this directory the current directory of the script. (Use a command to change into this top directory.) c) Create new (empty) subdirectories named "one", ".two", ".three *", and "." under the top directory. (These names have blanks and other special characters in them, but no quotes; be precise.) We will call these four "subdirectories" of the top directory. d) In the top directory, create a file named "happy" containing the three words "happy $hopping day" on a line by itself. Note the use of a dollar sign instead of the letter "s" in "$hopping", and note that each word has exactly two blanks that separate it from its neighbour. (Hint: Use redirection to create this file with the words in it.) e) Use commands to create identical "happy" names for the file "happy" in all the four subdirectories. The file will now have five names. f) Show a long listing of the file "happy" in the third subdirectory. The link count on "happy" should be five. The file should be exactly 21 characters long. (If not, go back and fix it!) g) Use the checksum command from the course notes to show a checksum of the file "happy" in the fourth subdirectory. (The sum should be 11409.) h) Rename the file in the first subdirectory to have the name "". Rename the file in the second subdirectory to have the name "*a*". (These names have special characters in them, but no quotes.) i) Append the line "no prompt C:\" to the file in the first subdirectory. The line contains a backslash character, but no quotes. (Hint: Use redirection.) The file will now have two lines, a size of 35 characters, and a checksum of 40700. (Fix it if this is not true!) j) Sort in reverse all the non-hidden files in all subdirectories into the output file "sorted" in the top directory. (You get a bonus mark for using GLOB patterns that match all the subdirectory names and all non-hidden files inside them. Do not match the short hidden directory names "." or "..".) The file "sorted" will have eight lines, 24 words, 140 characters, and a checksum of 19188. (Fix it if this is not true!) Display this information. k) Process the file "sorted" to remove duplicate adjacent lines and save the result in the file "unique" under the third subdirectory. The file "unique" will have two lines, six words, 35 characters, and a checksum of 60406. Display this information. l) Pick off the first line from "unique" and save it under the second subdirectory using the name "head". The file "head" will have one line, three words, 14 characters, and a checksum of 60599. m) Pick off the last line from "unique" and save it under the fourth subdirectory using the name "tail". The file "tail" will have one line, three words, 21 characters, and a checksum of 11409. Display this information. n) Use the difference command to compare the file in the first subdirectory with the file "unique". Save the output in the first subdirectory under the name "differences". The file "differences" will have four lines, 10 words, 54 characters, and a checksum of 33287. Display this information. o) Create one empty file named "empty linked" with four names, one name in each of the four subdirectories. The name has a blank in it. The link count on these file names will be four. p) Create four different empty files named "empty no linked XX YYYY", one file in each of the four subdirectories, where XX is replaced by the process ID of the currently executing script, and YYYY is replaced with the userid of the person executing the script. The name also has blanks in it. The link count on each of these separate files will be one. (For example, the file name may be "empty no linked 27461 alleni99" if the process ID is 27461 and the USER running the script is alleni99. Hint: read Notes file shell_variables.txt) 6. Execute your script. Make sure it generates the correct output and no errors: $ ./myscript.sh Save the output of the script in a file "myscript.txt" and submit it for marking (see below). 7. Above each of the lines in your script, add a one-line (less than 80 characters!) comment explaining in your own words what the line that follows the comment does. Scripts without comments will not be marked. Submission ---------- Reference: datsubmit.txt - Using the datsubmit command Submit the output and the finished and labelled script file for marking as Exercise 03 on the Course Linux Server, using the following datsubmit command line: $ datsubmit 03 myscript.sh myscript.txt This "datsubmit" program will copy the files to me for marking.