----------------------- Exercise #4 for DAT2330 due April 16 ----------------------- -Ian! D. Allen - idallen@idallen.ca Global weight: 4% of your total mark this term Due date: 14h30 (2:30pm) Friday, April 16, 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 09h00 (9am) on Monday, April 19. 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 14h30 (2:30pm) Friday, April 16, 2004. ----------------- Exercise Synopsis ----------------- You will write a shell script to implement a date reminder service. When the script is executed, it will look in a reminder file and print lines matching the date. The date defaults to today; but, other user-supplied dates can be given on the command line: $ ./exercise04.sh 2004 Mar 30 Dentist appointment. $ ./exercise04.sh "April 2" 2004 Apr 2 DAT2330 midterm test. $ ./exercise04.sh tomorrow 2004 Mar 31 Dentist appointment. You could put this script in your .bash_profile and have it remind you of important dates, birthdays, tests, etc. -------------- Specifications -------------- Write an executable shell script on the Course Linux Server named exercise04.sh that will implement the following specifications. The script will be divided into two sections: I. Handle command line input, user input, and input validation. II. Loop reading lines (from the date reminder file) and print the reminders that match the user-supplied date. The two sections are described in detail below. Follow the 9-part script format described in Notes file: script_style.txt Build exercise04.sh ONE STATEMENT AT A TIME and test it after each line. You will not be able to make it work if you write a dozen lines and then try to debug it. You will find the "-v" and "-x" options to bash helpful to debug your script: $ bash -u -v ./my.sh Syntax description: ------------------- a) The first argument to this script is an optional date string. (If only one argument is present, assume it is the date string.) The user can set the date string to be any STRING accepted by the "--date=STRING" option to the GNU/Linux "date" command, including strings such as "yesterday", "tomorrow", "next week", "last month", etc. See Appendix "A" to this exercise, below, for details. b) The second argument is an optional reminder file name. The syntax of lines in the reminder file is given in Appendix "B" to this exercise, below. (To supply the reminder file name as a second argument, you must also supply a date string as the first argument.) I - Input and Validation: ------------------------- 1. Print a good error message and exit the script if there are too many arguments. [4 lines] 2. The first optional argument to this script is a date string: If the first argument is present on the command line, use the first argument as the date string value. If first argument is missing, the date string value should be set to the string "today". [5 lines] 3. Print a good error message and exit the script if the date string is empty (null string - no characters). (Note: This is not a file or a file name. The date string argument is a string of characters.) [4 lines] 4. The second optional argument to this script is a reminder file name: Here is the pseudocode for this step [14 lines]: If the second argument is present on the command line Use the second argument as the reminder file name. Otherwise: Set the reminder file name to be the default name "reminders.txt". If that reminder file name isn't the name of an existing plain file Display a warning message that the file isn't found If standard input is a terminal Get a new reminder file name from the user. (... but quick-exit the script if given EOF by the user.) Otherwise: Set the reminder file name to be empty Endif Endif Endif You can test whether or not standard input is coming from a terminal (from your keyboard) using the return status of the "tty" command. Suppress or throw away the standard output of the tty command; check only the return status of the command. Note that the reminder file name may end up being empty (the null string). That's okay. If the file name is empty, the script will read date reminder lines from standard input instead of from the file. 5. If the file name entered by the user is *not* an empty string, do the actions in this step [16 lines]: Validate the file: make sure it is a non-empty, readable, plain file. Exit with a good error message if any of these tests fail. Open the file as standard input to the script using this new shell input redirection syntax: exec < XXXX || exit 1 where XXXX is replaced in your script by the user-entered file name. After executing the above "exec" line, the rest of the script will behave as if you had typed "