====================================== Problems and Answers for Test 3 - Unix ====================================== -IAN! idallen@ncf.ca Preamble -------- You will need to be running Floppix with networking to do this test. You must have network access to use telnet and ftp to reach the machines mentioned in this test. The test_three directory used in some of the scripts, below, is a sub-directory of your HOME directory on the Test Machine. This directory will be created by your instructor before you start your test. Your directory test_three is where you must put your scripts and your script output if you want the instructor to mark them. Your scripts do not have to create this directory; you may assume it exists any time a script specification requires you to use it. Only scripts and output found under directory test_three on the Test Machine will be marked. Only correctly-spelled script and file names will be marked. Your scripts must start with a valid Script Header, as given in class and in the Week 7 notes. You do not have to detect or handle errors in the scripts you write on this test. Error handling will be covered in Chapter 11. For example, your script does not need to check to see if a file or directory already exists before it tries to make one. Many of the scripts you write during this test will not run without errors if you run them twice; because, the scripts will have created files and/or directories and a second attempt to re-create the same files and/or directories may generate error messages. Make sure you clean out the results of one script run before you test your script a sec­ ond time. --------------------- Problem A - Marks: 12 --------------------- On the Test Machine: Write an executable shell script named several_things.sh that will do the following actions (in this order): 1. Display the current date. 2. Put the pathname of the file containing your Unix mail into a file named current_mail.txt in the current directory. (Use an existing keyword variable for the mail file pathname.) 3. Put the last three lines of the Unix password file into a file named one_out.txt in the home directory of the person running the script (not in the current direc­ tory). (Use an existing keyword variable for the directory name.) 4. Have the script set the permissions on the one_out.txt output file to be (a) read only for owner, (b) write only for group, (c) nothing for others. #!/bin/sh -u # $0 (no arguments) # This script does several small miscellaneous things. # -IAN! idallen@ncf.ca export PATH=/bin:/usr/bin umask 022 date echo "$MAIL" >current_mail.txt tail -3 /etc/passwd >"$HOME/one_out.txt" chmod o=r,g=w,o= "$HOME"/one_out.txt -------------------- Problem B - Marks: 8 -------------------- On the Test Machine: Write an executable shell script named user_finger.sh that will do the following actions (in this order): 1. Display the current date. 2. Display the users on the current machine with their login times. 3. Run finger to the Linux Test Machine (use the IP address) to show all logins, and delete from the dis­ play the logins that are by the user running the script. The output must not contain lines for the user name running the script. (Hint: man whoami) #!/bin/sh -u # $0 (no arguments) # This script fingers the test machine and shows users who are not me. # -IAN! idallen@ncf.ca export PATH=/bin:/usr/bin umask 022 date who finger @205.211.32.171 | grep -v "$(whoami)" --------------------- Problem C - Marks: 11 --------------------- On the Test Machine: Write an executable shell script named input_counter.sh that will do the following actions (in this order): 1. Display the current date. 2. Prompt to read a line of input from the script user. 3. Read a line of input. 4. Output this sentence (with this exact punctuation): You entered: "XXX" where XXX is the input line entered. 5. Output this sentence: Your line contains YYY characters. where YYY is the count of the number of characters in the line of input. #!/bin/sh -u # $0 (no arguments) # This script reads a line of input and counts the characters in it. # -IAN! idallen@ncf.ca export PATH=/bin:/usr/bin umask 022 date echo 1>&2 "Enter a line of input:" read input echo "You entered: \"$input\"" echo "Your line contains $(echo "$input" | wc -c) characters." --------------------- Problem D - Marks: 14 --------------------- On the Test Machine: Write an executable shell script named who_selector.sh that will do the following actions (in this order): 1. Display the current date. 2. Output this sentence (with this exact punctuation): Your pattern was "XXX". where XXX is the text string that is the first argument to the script. 3. Output this sentence (with this exact punctuation): The file name was "YYY". where YYY is the text string that is the second argu­ ment to the script. 4. Run the who command and select only lines that contain the pattern that is the first argument to the script. Put the resulting output into the file name that is the second argument to the script. 5. Output this sentence (with this exact punctuation): The pattern matches ZZZ lines. where ZZZ is the count of the number of lines in the file you created. #!/bin/sh -u # $0 pattern filename # This script looks for a pattern in who output and puts output in filename. # -IAN! idallen@ncf.ca export PATH=/bin:/usr/bin umask 022 date echo "Your pattern was \"$1\"." echo "The file name was \"$2\"." who | grep "$1" >"$2" echo "The pattern matches $(wc -l <"$2") lines."