E N D
CSI 135 Homework #4 review
Question #1 • Use the cat command to display the contents of a file called "sayings" which you can fetch (fetch –ishs sayings) from your instructor’s home directory on vader. Next, use 'grep' on this file to display only those lines that contain the word ‘not’ and then count these lines using 'wc'. What was your command and how many lines did you find?
Answer #1 cat sayings | grep not | wc –l 13 grep not sayings | wc –l 13 grep-c not sayings 13
Question #2 • Do the same thing again but this time select only those lines that contain “not” as a complete word. For example, instances of “note” or “notable” should not be considered. What command did you use? • Hint: Your textbook and/or help files on vader will provide information on how to do this.
Answer #2 cat sayings | grep -w not | wc -l 11 grep–w not sayings | wc–l 11 grep-wc not sayings 11
Question #3 • Use the grep, awk, cut and/or wc commands on a single line to find out how many accounts in /etc/passwd file share your GID. Show your command. Hint: Identify the delimiter in the target file.
Answer #3 $ awk'{ print $1 }' /etc/passwd | grep :200: | wc–l 801 This answer is very close! What’s wrong with it?
Answer #3 $ awk-F: '{print $4}' /etc/passwd | grep -w 200 | wc -l 800 Look only at group field Field delimiter csi135b11:x:1737:200:Intro to UNIX-Linux - section 400:/u/students/fall10/csi135/b11:/bin/bash
Question #4 • Fetch the file named “whoison” from your instructor using the command “fetch –I shswhoison”. Explain what the whoison script is doing.
Answer #4 #!/bin/bash echo hello, $USER echo Look who is logged in! echo =========================== who | awk '{print $1}' | sort | uniq echo ===========================
Answer #4 who | awk '{print $1}' | sort | uniq • wholists all the people logged on, • awk '{print $1}' pulls out the 1st field of the who output, • sort puts it in alphabetical order • uniq eliminates duplicate lines
Question #5 • Display the first four fields in every line of the /etc/passwd file using the cut command. Show your command.
Answer #5 $ cut –d: -f 1-4 /etc/passwd root:x:0:0 bin:x:1:1 daemon:x:2:2 adm:x:3:4 lp:x:4:7 sync:x:5:0
Question #6 • The 'file' command will make a guess and report to you what it thinks a file contains. Use the file command and one other command to display only those files in /usr/local/bin on vader are symbolic links. What command did you use?
Answer #6 $ file /usr/local/bin/* | grep symbolic /usr/local/bin/fetch: symbolic link to `fetch5' /usr/local/bin/ko: symbolic link to `killother' /usr/local/bin/perl: symbolic link to `/usr/bin/perl' /usr/local/bin/submit: symbolic link to `submit2' /usr/local/bin/wx: symbolic link to `wx3'
Question #7 • Every process has an environment containing variables and values defined for the process to use. The 'env' command displays the exported portion of your process environment. What Unix command would you use to list just the names of the variables?
Answer #7 env | cut -f 1 --delimiter== env | awk '{print$1}' | cut -d= -f1 env | awk -F= '{print $1}'
Question #8 • You can use the command 'read a b c' to input values for the a, b, and c variables from the keyboard. The shell waits for you to press the "Enter" key before going on. If you type additional text, the strings will be assigned to the last variable named in your read command. Try this and then use the 'echo $a $b $c' command to display the values just assigned. If you use the same number of variable names as words in the input sentence, each word will wind up assigned to a different variable. Considering this, how would you read a five word sentence in a script and then output the same sentence to the screen, but with the word order reversed? What commands would you use?
Answer #8 $ read a b c d e This sentence has five words $ echo $e $d $c $b $a words five has sentence This
Answer #8 $ read a b c d e This sentence has more than five words $ echo $e $d $c $b $a than five words more has sentence This
Question #9 • You can include the output of one command in another one using $( ) around it. For example: echo My directory contains $(ls) will list the contents of your current directory with the rest of the phrase in front. Use output redirection to create a file that contains the line 'TODAY IS ‐‐ ' followed by today's date and time by using echo, the date command and redirection. What command did you use to create the file?
Answer #9 echo "Today is -- $(date)" Today is -- Fri Oct 8 20:31:14 EDT 2010
Question #10 • What command could you use to list every line in the man page for the cut command that ends in the letter “s”?
Answer #10 $ man cut | grep s$ cut - remove sections from each line of files Mandatory arguments to long options are mandatory for short options select only these bytes select only these characters -n with -b: donât split multibyte characters do not print lines not containing delimiters