1 / 24

Homework #4 review

CSI 135. Homework #4 review. Question #1.

tim
Download Presentation

Homework #4 review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSI 135 Homework #4 review

  2. 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?

  3. Answer #1 cat sayings | grep not | wc –l 13 grep not sayings | wc –l 13 grep-c not sayings 13

  4. 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.

  5. Answer #2 cat sayings | grep -w not | wc -l 11 grep–w not sayings | wc–l 11 grep-wc not sayings 11

  6. 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.

  7. Answer #3 $ awk'{ print $1 }' /etc/passwd | grep :200: | wc–l 801 This answer is very close! What’s wrong with it?

  8. 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

  9. Question #4 • Fetch the file named “whoison” from your instructor using the command “fetch –I shswhoison”. Explain what the whoison script is doing.

  10. Answer #4 #!/bin/bash echo hello, $USER echo Look who is logged in! echo =========================== who | awk '{print $1}' | sort | uniq echo ===========================

  11. 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

  12. Question #5 • Display the first four fields in every line of the /etc/passwd file using the cut command. Show your command.

  13. 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

  14. 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?

  15. 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'

  16. 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?

  17. Answer #7 env | cut -f 1 --delimiter== env | awk '{print$1}' | cut -d= -f1 env | awk -F= '{print $1}'

  18. 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?

  19. Answer #8 $ read a b c d e This sentence has five words $ echo $e $d $c $b $a words five has sentence This

  20. 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

  21. 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?

  22. Answer #9 echo "Today is -- $(date)" Today is -- Fri Oct  8 20:31:14 EDT 2010

  23. 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”?

  24. 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

More Related