----------------------- Lab #04 for NET2003 due February 12, 2008 (Week 6) ----------------------- 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: 3% of your total mark this term. Due date: before 14h00 (2pm) Tuesday February 12 (Week 6) The deliverables for this lab exercise are to be submitted online on the Course Linux Server using the "netsubmit" method described in the lab exercise description, below. No paper; no email; no FTP. Late-submission date: I will accept without penalty lab exercises that are submitted late but before 14h00 (2pm) on Wednesday, February 13. After that late-submission date, the lab exercise is worth zero marks. Lab 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. Lab Synopsis: Update your list of known Unix commands. Create an executable shell script. NOTE: For full marks, keep your lines shorter than 80 columns in this course. Short lines allow for easy printing and side-by-side comparison of files on a screen. Where to work: See previous labs. Easy access to Course Notes: See previous labs. ---------------------------------- Lab Details (on some Linux system) ---------------------------------- You may prepare this file on any Linux system. You can only submit it on the Course Linux Server. Sample output is given for the Course Linux Server. *) For full marks, keep your lines shorter than 80 columns in this course. Short lines allow for easy printing and side-by-side comparison of files on a screen. *) You may find it useful to create separate directories in your linus server account to store the files for each lab exercise, e.g. $ mkdir lab4 $ cd lab4 $ vim lab04cmds.txt $ vim lab04script.txt Part I - lab04cmds.txt ------ Update your list of commands, following the model from Part I of Lab 2. The new file name to submit must be: lab04cmds.txt Work smart - don't duplicate the work you've already done: You can use VIM to open two files side-by-side and cut/paste between. ( :help ^W ) ( :vsplit filename ) Part II - lab04script.sh ------- You will build an executable shell script containing a commented list of Unix commands to be executed. In shell scripts, lines starting with octothorpe characters ('#') are ignored as comment lines. 1. Fetch a copy of the argv.sh.txt file from the course notes directory (copy the file) and rename your copy to be: lab04script.sh Make the file executable using the chmod command (note the plus): $ chmod +x lab04script.sh 2. Verify that the copied script executes correctly and without error: $ ./lab04script.sh a b c ./lab04script.sh: This script has 3 command line arguments. Argument 0 is [./lab04script.sh] Argument 1 is [a] Argument 2 is [b] Argument 3 is [c] 3. At the top of the lab04script.sh file, fix the existing Assignment Submission label comment to be your own label. Make sure the lines of the label all start with the shell comment character '#'. 4. Delete the line (line 2) starting with "# Display on standard error ...". 5. At the top, under "Syntax", replace "[args...]" with "(no arguments)" so that the line reads: # $0 (no arguments) This script takes no command line arguments. 6. At the top, under "Purpose", replace the paragraph with a short description of what this script does (come back to this - see below). Don't forget this step! 7. Change the line starting with PATH= to be this exact line below by adding "/sbin" and ":" to the start of the PATH string: PATH=/sbin:/bin:/usr/bin ; export PATH Do not add any blanks or quotes to this line. Make sure you see the two characters ":" and ";" as different. (Adding /sbin to the PATH allows the shell to find executable commands in the /sbin directory as well as in /bin and /usr/bin .) At this point, you might re-execute the script to make sure you haven't introduced any typing errors. The output should be the same as above. There should be no error messages on the screen. 8. Delete everything below and after the line "umask 022" in the file. 9. Add a blank line after "umask 022". 10. Delete all the "I!" comment lines. Never submit these lines in your own scripts - they are "instructor" comments from me to you. 11. You will now add, to the bottom of this executable script, three lines for every section in this question. The first line will echo the question number onto the screen in the exact format shown. The second line will be a shell comment that will not print on the screen. The third line will be a shell command that will do the action required by the question. The output from the command will appear on the screen, if the command generates output. (Many Unix/Linux commands work silently and have no output on the screen.) For example, if asked to display "the current time and date" you would echo the question number, enter a comment line, and enter the correct Unix/Linux command into the bottom of the script file like this: echo "--- Question 11b" # display the current date and time date Use the exact format and syntax shown for the echo line that shows the Question number. (Use the cut-and-paste features of vim to copy the line for the next question; don't re-type it every time!) Keep the comment short - exactly one line, less than 80 columns. You may want to open up another login 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, and run the script after each addition. As you write the script, leave one blank line between each question you answer, to make the script source more readable. The result will be groups of three lines, separated from one another by one blank line. Test your script periodically (after each edit) by saving it and running it from the command line (perhaps in a different window): $ ./lab04script.sh Make sure there are no error messages. Run the script after each edit you add; that way you debug only the few lines you added. NOTE: Do not use any "change directory" commands in your script unless the instructions ask for it explicitly. Do not add commands or create output that is not required. Do only the commands listed, in the order listed. If you can't answer a question: echo the question number, add the comment line, but leave out the actual command itself. Here are the individual questions to add to your executable script. Find executable commands that look up and produce the given output. Enter the Unix commands into the script and make sure they generate the correct output when you execute the script. All the command names needed are in the Notes file: unix_command_list.txt The acronym CLS stands for "Course Linux Server - "10.50.254.148".) 11. Execute your script as you add each of the command lines below. Make sure the script generates the correct output and no errors when you execute it: $ ./lab04script.sh a) Display this text on your screen: This is an executable shell script. (Note: Spelling and punctuation count! Be precise.) b) Show the name of this computer (the one on which you are working). c) Show the calendar for September 1752. d) Show the disk usage (file space usage) for the /bin directory. (The CLS shows approximately 4836 blocks.) e) Show a list of all the absolute pathnames, recursively, that are under the directory /etc/X11/fonts (The CLS shows approximately 9 absolute pathnames, including five directories.) f) Execute a command that shows what type of file /bin/ls is. (It should tell you that it is an ELF 32-bit LSB executable.) g) Display a count of only the lines and words in the file /etc/passwd. (The command that does this kind of thing usually also prints the number of bytes - make sure only the lines and words are counted.) (The CLS shows least 50 lines; maybe more by the time you read this.) h) Display on the screen only the last 2 lines from the /etc/passwd file. i) Display on the screen only the first 2 lines from the /etc/passwd file. j) Search in the titles of all manual pages for the keyword "wireless". (The CLS displays about 7 manual page titles.) k) Display only the line(s) containing the string "root" that is/are found in the file /etc/passwd (At least one line) l) Using the two command names given in the course notes, display the checksums of the bytes in the /bin/ls file. (On the CLS, one output starts with the digits 96 and the other with the digits 22.) m) Remove a pathname named "newdir" recursively, including all files and subdirectories that might lie under it. Find a way to suppress the error message you get if the "newdir" directory is nonexistent. n) Create a new (empty) directory named "newdir" (without the quotes). This directory making command will always succeed, if you have done the previous step correctly. If you get an error, check the previous step. o) Make "newdir" the current directory. In other words, change into the "newdir" directory. p) Generate the current date and time and put the output into a file named newfile in the current directory. (See Notes: redirection.txt) q) Create a new (empty) directory named "subdir". r) Make "subdir" the current directory s) Execute a command that shows the current working directory and append the result to the "newfile" file you created above. The current directory name should end in "newdir/subdir". Use a relative path to "newfile". t) Move the file named "newfile" that you created above into the current directory under the four-character name: .foo Use a command that does a move/rename operation, not a copy. Use a relative path to "newfile". u) Execute a command that shows only the current TCP connections to this machine in numeric format (no host names) and append the output to the .foo file. v) Now append to .foo the network interface information for eth0. w) With only one single command, show the directory listing (short form) of *all* names (including hidden) in the current directory and in the parent directory. (Three names will display under the current directory; three names will display in the parent directory.) x) Display the checksum of all files ending in "sh" in the directory /bin. Use the "sum" command, and use a shell GLOB pattern to generate the file names. (On the CLS, the output is four lines, with five-digit checksum values starting each line.) y) Display all the file names (just the names) under /usr/bin that contain the string "ssh" anywhere in their names. z) Last command: Cause the script to exit. All the command names needed are in the Notes file: unix_command_list.txt 12. Verify the script format. Above each of the lines in your working script, make sure you have added a line that echoes the question number in the exact format given. Under that echo line, make sure you have a one-line (less than 80 characters!) comment explaining in your own words what the command line that follows the comment does. Each answer will look similar to this: echo "--- Question 11z" # exit the shell script with a good status of zero exit 0 In your script source, leave one blank line between each question you answer, to make the script readable. (You may also echo blank lines to the screen to separate the output sections, if you wish.) 13. Format check: To verify that you have used the correct format for each of the "echo" lines before each question, you can use the following command lines to count or display the number of lines that match the pattern. Pay attention to the use of single quotes: $ grep -c -e 'echo "--- Question 11' lab04script.sh 26 $ grep -e 'echo "--- Question 11' lab04script.sh ... the 26 lines for 11a through 11z should print here ... The count should be 26 lines, one for each of 11a through 11z. If it isn't 26 lines, go back and fix your script. 14. Go back and finish the Purpose section comment at the top of the script. Scripts without your added comments will not be marked. NOTE: For full marks, keep your lines shorter than 80 columns in this course! Submission ---------- 15. Submit the above two files (see below). Submission Standards: See Lab #1 for details. A. Make sure both files contain an Exterior Assignment Submission label. For full marks, lines must be shorter than 80 columns. B. Submit your files for marking as Lab 04 using the following *single* netsubmit command line exactly as given here: $ netsubmit 04 lab04cmds.txt lab04script.sh Always submit *all* files at the same time for every submission. Files submitted under the wrong names are worth zero marks. P.S. Did you spell all the assignment label fields and file names correctly?