----------------------- Exercise #3 for DAT2330 due September 17 ----------------------- -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: 1% of your total mark this term Due date: 12:00 noon Wednesday, September 17, 2003. The deliverables for this exercise are to be submitted online on ACADUNIX using the "submit" 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 before 10h00 (10am) on Friday, September 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 email sent to you after the late-submission date. A sample answer will be posted online after the late-submission date. Exercise Synopsis: This week you will create your first executable "shell script". You will modify the script to have it execute some simple Unix commands using GLOB (wildcard) patterns as arguments. References and Readings: Running Linux: Chapter 4. Where to work: Do your Unix command line work on the ACADUNIX computer. Exercise Details: 1. On ACADUNIX make a new directory under your home directory in your account. You may use any name you like; but, avoid blanks, slashes, and special characters until you know how to "quote" names properly (see file quotes.txt). 2. Move into the new directory you just created. (i.e. Make the directory you just created your current working directory.) 3. Verify that you are in the subdirectory you just created. Stay in this subdirectory. The outputs of the next few commands should create files in this new subdirectory (not in your home directory). 4. Create a file myscript03.sh in the current directory containing these two lines (start each line in the first column; no spaces): #!/bin/sh -u echo 'This is my first shell script.' There should be no blank lines in the file, no leading blanks at the start of any line, and no trailing blanks at the end of the lines. There are two single quotes and one period on line two. Line one has one blank in it. If you do the file correctly, it will have these statistics: $ wc myscript03.sh 2 9 51 myscript03.sh $ sum myscript03.sh 43421 1 Make sure you get exactly the same numbers in your output. 5. Make the file executable using this command line (see text p.104): $ chmod +x myscript03.sh 6. Execute the script in the current directory and verify the output: $ ./myscript03.sh This is my first shell script. Make sure you get the same output. 7. Edit the myscript03.sh file and put your Exterior Assignment Submission label between lines one and two (after line one, before line two). Prefix each line of the Label with a "#" character to make it a comment in the script. (Make the change on one line and then move to the next line and use the vim "." command to repeat the change again on the new line.) The script will now have at least nine lines. All the lines except the last line will start with a "#" character in column 1. Save the modified script. Execute it again to make sure it produces exactly the same output as before (no errors!). 8. Edit the myscript03.sh file again. At the end of the file, after the echo command line, add a new command line to copy the file named "motd" from in the directory "/etc" into the current directory under the name "message_of_the_day.txt". Use an absolute pathname for the first argument to the cp command and a relative pathname for the second argument. (The vim command "o" starts a new line; or, you can use command "a" at the end of the previous line.) Save the modified script. Execute it to make sure it works. (Do you get a file named message_of_the_day.txt in the current directory?) 9. To the end of the myscript03.sh file, add a single command line showing the permissions, owner, and modify date of the same two file names that you used as arguments to the cp command in the previous line. Save the modified script. Execute it to make sure it works. (Do you see a listing of both files on your screen?) 10. To the end of the myscript03.sh file, add a command line showing the permissions, owner, and modify date of all file names starting with the letters "r" or "c" and ending with the two letters "sh" under the directory "/bin". Save the modified script. Execute it to make sure it works. (Do you see a long listing of five file names?) 11. Edit the myscript03.sh file and add new comment lines (starting with "#") above each command line, explaining what each command line does. Do not change the command lines; just add new comment lines to the script. The file has an echo command, a copy command, and two long listings; you must add four comment lines. Put a blank line before each of the four comment lines that you add. 12. The finished myscript03.sh file will have the "#!/bin/sh -u" line at the very top, followed by 7-8 lines of assignment label comments, followed by a commented echo command, a commented copy operation, and two commented long listings. It will be at least 20 lines long (counting the added blank lines). Make sure the script works! 13. Create a file named answer03.txt with your assignment label in it. At the end of the answer03.txt file, after the label, answer the following questions: a) True or False? In the shell command line "cat /usr/*" the 'cat' command receives one command line argument. b) True or False? If a shell GLOB pattern (i.e. ?, *, [abc]) on a command line fails to match anything, the shell deletes the pattern and passes nothing to the command. c) True or False? The shell GLOB pattern [0-9] matches a name that is a digit zero, a dash, or a digit 9. d) True or False? The shell GLOB pattern ???? matches a name that is at least four characters (or more) long. 14. Submission. Submit both completed files for marking as Exercise 03: $ ~alleni/bin/submit 03 myscript03.sh answer03.txt For Exercise 03, always use "03" as the first argument to "submit".