--------------------------------------------------------------------- DAT2330 - Assignment 4 - Acadaix - 30 marks total - 5% of course mark --------------------------------------------------------------------- Ian D. Allen - idallen@ncf.ca Read the whole assignment before you start. Assignment: 1. Prepare the given shell scripts in the specified directory in your account on ACADAIX. Read the whole assignment before you start. 2. Using my program (below), create a file containing a listing of all the scripts. Print the listing file and hand it in. Due: 1. The scripts you create for this assignment must be dated before 8:00am Friday March 10. Scripts modified after 8am will not be marked; so, do not edit them or change them after 8am on Friday. 2. The print-out on paper that you must hand in is due in my hand or under my office door at 8:00am Friday March 10 (the same day). Late submissions of the print-out incur a 20% per day penalty. Unix is a case-sensitive operating system. Command and file names given in this assignment are exact with respect to spelling and upper- and lower-case letters. (See your Linux text, page 21.) Read the whole assignment before you start. --------------------------------------- Part I - Preparing the Answer Directory --------------------------------------- A. Set Permissions Change the permissions on your ACADAIX home directory to 711 before you begin this assignment: chmod 711 $HOME Marks will be deducted for incorrect home directory permissions. B. Create an Answers Directory Create a sub-directory in your account whose name is your 9-digit student number followed by five or more random letters that you choose, e.g. a directory name of the form "040837625wugga" or "040968736dogbark". Create a directory, not a file. I will call this new directory your "Answers Directory". C. Create a README file in your Answers Directory a) On the first line of a new file named README located inside your Answers Directory, enter the full Unix pathname of the README file. Spell it correctly: I will use this name to find your answers directory. If the name is wrong you get no marks! The first line of your README file will look similar to this: Full Pathname: /shome/abcd0001/040958736wugga/README b) At the bottom of this same README file, enter a completed Ian Assignment Label. (You might use a text editor to do this.) ----------------------- Part II - Write Scripts ----------------------- Note 1: Read the whole assignment before you start. Note 2: Before you begin this Assignment, you should have an Answers Directory in your account containing one file named README. Note 3: Put all the scripts you write for this assignment inside this Answers Directory, or else they will not be marked. Note 4: The names of the scripts must be exactly the names given in each question. Scripts with other names will not be marked. Note 5: The output requested is marked for precise spelling and accuracy. Marks are deducted for missing spaces, incorrect capitalization, and output that doesn't look exactly like what is requested. 1. (Marks: 2) Script name: myhello Write an executable shell script named "myhello" that prints the phrase "*** Hello World ***". Sample usage: $ ./myhello *** Hello World *** 2. (Marks: 4) Script name: whoami Write an executable shell script named "whoami" that prints the following sentence that includes the account name of the person who runs the script: "Your account's name is ." followed by the following sentence that indicates the home directory: "Your account's home directory is ." Sample usage for userid abcd0001: $ ./whoami Your account's name is abcd0001. Your account's home directory is /shome/abcd0001. Hint: The account name is in a shell environment variable named USER. 3. (Marks: 6) Script name: dirname This script is modelled after the "simple2.sh" script from my dat2330 directory, demonstrated in the Labs this past week. Study it for the syntax for the "test" command you will need here. See also the use of the "test" command in the text on p.372. Write an executable shell script named "dirname" that prints the the absolute path of the current directory and the following sentence if the current directory is writable by the person running the script: ": The current directory is writable by ." Otherwise, print: ": The current directory *** isn't **** writable by ." ( is the name of the script, derived from positional parameter number zero [text, p.333].) Sample usage for userid abcd0001: $ ./dirname /shome/abcd0001/040985746wugga ./dirname: The current directory is writable by abcd0001. $ cd /etc $ ~/040985746wugga/dirname /etc /shome/abcd0001/040985746wugga/dirname: The current directory *** isn't *** writable by abcd0001. $ cd /tmp $ ~/040985746wugga/dirname /tmp /shome/abcd0001/040985746wugga/dirname: The current directory is writable by abcd0001. 4. (Marks: 8) Script name: sdiff Write an executable shell script named "sdiff" that echoes its arguments and then says whether the first two arguments on the command line are identical, or not. The script should not execute unless two arguments are given. The script must be able to handle arguments that contain blanks and other special characters. Sample usage: $ ./sdiff ./sdiff: 1: 0403-041 Parameter not set. $ ./sdiff dog dog Argument one is: dog Argument two is: dog 'dog' is the same as 'dog'. $ ./sdiff dog cat Argument one is: dog Argument two is: cat 'dog' *** isn't *** the same as 'cat'. $ ./sdiff "some spaces" "some spaces" Argument one is: some spaces Argument two is: some spaces 'some spaces' is the same as 'some spaces'. $ ./sdiff "*" " * " Argument one is: * Argument two is: * '*' *** isn't *** the same as ' * '. Pay attention to the wording and punctuation of the output examples. 5. (Marks: 10) Script name: threeway Write an executable shell script named "threeway" that echoes its arguments and then says which of the three arguments on the command line are the same. - The script must be able to handle arguments that contain blanks and other special characters. - The script must exit with a good return status if there are no errors. (Study how "chkargs" does this on page 372.) - The script should print an error message and exit with a non-zero status if more or less than three arguments are given. The error message must start with the name of the script (from positional parameter zero). Hint: Study the program named "chkargs" on pages 371 and 372 in Chapter 11. Look under "Shell variables" in the index for the explanation of the "$#" variable. Sample usage: $ ./threeway dog dog dog Argument 1 is: dog Argument 2 is: dog Argument 3 is: dog Argument 1 'dog' is the same as argument 2 'dog'. Argument 1 'dog' is the same as argument 3 'dog'. Argument 2 'dog' is the same as argument 3 'dog'. $ ./threeway dog cat cow Argument 1 is: dog Argument 2 is: cat Argument 3 is: cow $ ./threeway dog pig dog Argument 1 is: dog Argument 2 is: pig Argument 3 is: dog Argument 1 'dog' is the same as argument 3 'dog'. $ ./threeway " ?* " " .* " " .* " Argument 1 is: ?* Argument 2 is: .* Argument 3 is: .* Argument 2 ' .* ' is the same as argument 3 ' .* '. $ ./threeway ./threeway: Wrong number of arguments ./threeway: Expecting 3, found 0 $ ./threeway one two three four five ./threeway: Wrong number of arguments ./threeway: Expecting 3, found 5 $ ./threeway /bin/* ./threeway: Wrong number of arguments ./threeway: Expecting 3, found 615 Pay attention to the wording and punctuation of the output examples. ---------------- Part III - Notes ---------------- Approximate script lengths (line count without comments or blank lines): 1 myhello 2 whoami 6 dirname 7 sdiff 18 threeway These lengths are for my answers; your lengths may differ (but not too much!). Marking information for shell scripts: Here are the things I look for in a Unix shell script: 1) Script starts with a proper "!" interpreter comment line. (Chapter 10 - "Specifying a Shell") 2) Script contains a comment about what it does and who wrote it. (Ian's rules for program documentation.) 3) Script has proper form (e.g. variables are quoted, indentation is correct, spelling is correct, etc.) (Chapter 10 - "User-Created Variables") 4) Script works and prints exactly what is required: a) There are no error messages. b) The output is complete, as specified. c) The output is spelled and punctuated exactly as given in the specification and example usage. d) If an exit status is specified, the script exits with the correct exit status. (See "exit status" in the index.) ----------------- Part IV - Hand In ----------------- a) Make sure you did Steps A, B, and C. b) Make sure the correct absolute pathname of your README file appears at the top of your README file. Make sure the README file and all your answer files are all in your Answers Directory. c) Change directories into your Answers Directory. Execute this program in your Answers Directory: $ ~alleni/dat2330/mark_a4 If everything works, you will find a new file named "A4-Answers.txt" in your current directory, containing a listing of the README file followed by all your other answer files. Print this file on 8.5 x 11 paper, according to my Assignment Submission guidelines. (See my course notes file on how to print from ACADAIX.) d) Submit your printed assignment to me (or under my office door) before the due date and hour given above. ---------------- Part V - WARNING ---------------- Do not modify or change any files in the Answers Directory after the stated submission time for this assignment! Files edited or changed after the submission time will not be marked.