This is the preamble and three script questions from the Fourth Unix Test. -IAN! idallen@ncf.ca July 2001 ================== Part A: Unix work ================== Your directory test_four is where you must put your scripts and your script output if you want the instructor to mark them. The test_four directory is a sub-directory of your HOME directory on the Test Machine. This directory will be created when you log in to the Test Machine. Only scripts and output found under directory test_four on the Test Machine will be marked. Only correctly-spelled script and file names will be marked. Your scripts must follow the programming guidelines set out during the course, including helpful error messages, correct exit codes, and comments. These are significant scripts. Write your scripts "one line" at a time! ------------------------- Unix Task A - Marks: 15+2 ------------------------- On the Test Machine: Write an executable shell script named menu_script.sh that will do the following actions (in the order given below): 1. [2 marks] Expect a single pathname as the only command line argument to this script. If there is not exactly one command line argument, print a helpful error mes­ sage and exit. 2. [2 marks] Test to see if the pathname is an existing file. Print a helpful error message and exit other­ wise. 3. [2 marks] Test to see if the file is is executable. If it is executable, print a message to announce this fact, and then exit with a good status. 4. [2 marks] If the file is not executable, display the following menu on the screen: === Your Menu === 1. Display the first 7 lines of the file 2. Delete the file 3. Add execute permission to the file 4. Move the file to the /tmp directory q. Quit 5. [1 mark] Prompt for and read a menu selection. 6. [6 marks] Based on the menu selection, perform the indicated menu operation. Exit the script with a bad status if any of the menu operations fail. Exit the script with a helpful error message and a bad status if the menu selection is not recognized. 7. [Bonus 2 marks] Put the just the menu section part of the script into a loop, and keep prompting and accept­ ing menu selections for this file until Quit is selected. Do not exit the menu loop unless Quit is selected or one of the menu operations fails. ------------------------ Unix Task B - Marks: 4+2 ------------------------ On the Test Machine: Write an executable shell script named nonblank_script.sh that will do the following actions (in the order given below): 1. [4 marks] Display the count of the command line argu­ ments (if any), and the count of the number of charac­ ters present in all the command line arguments, without counting any of the blanks in or between any of the arguments. (Remove all the blanks before you do the count.) $ ./nonblank_script.sh one "two three" Count: 2 arguments and 11 non-blank characters. 2. [Bonus 2 marks] Fix the output not to use the plural on the words "arguments" or "characters" if there is only one argument or one character. $ ./nonblank_script.sh a Count: 1 argument and 1 non-blank character. No files are opened or read by this script. The script sim­ ply counts the non-blank characters in the strings that are actual command line arguments. ------------------------- Unix Task C - Marks: 24+2 ------------------------- On the Test Machine: Write an executable shell script named counter_script.sh that will do the following actions (in the order given below): 1. [4 marks] Expect a single pathname as the only command line argument to this script. (a) If there is no argu­ ment, prompt for and read the pathname. (b) If there is more than one argument, print a helpful error mes­ sage and exit. 2. [2 marks] Make sure the pathname itself (the name) does not contain any blanks. Print a helpful error message and exit otherwise. 3. [3 marks] Make sure the pathname itself (the name) starts with a lower-case letter and ends in a dot fol­ lowed by the three-character C++ extension: .cpp Print a helpful error message and exit otherwise. 4. [3 marks] Make sure the pathname is the name of an existing file and is readable. Print one or more help­ ful error messages and exit otherwise. 5. [4 marks] Make sure the very first line of the file is the C++ include line: #include Print a helpful error message and exit otherwise. Include the incorrect first line as part of the error output. (See Bonus, below.) 6. [2 marks] Make sure the file contains the string main somewhere. Print a helpful error message and exit oth­ erwise. (See Bonus, below.) 7. [4 marks] Count the number of words in the file; but, only count words on lines that do not contain the two- character string // (two adjacent slashes). Exit the script with a bad return status if the counting fails. 8. [2 marks] Generate output in the following exact for­ mat. Exact punctuation in the output is critical; extra blanks are acceptable. $ ./counter_script.sh abc.cpp File 'abc.cpp' contains 1234 non-comment (//) words. 9. [Bonus 2 marks] Do not display or generate any other output in these steps, other than the specified output and error message(s). No output, other than output actually specified in these instructions, should appear on the screen.