UNIX SHELL EXERCISES READING ------- Notes for lecture 3 SUPPLEMENTAL READING -------------------- Type the commands! Use "man" See course FAQ page EXERCISES --------- Suppose that the file "hello.c" contains the following program: #include main() { printf("hello, world\n"); } For exercises 1-7, describe what happens when you type the given commands. 1. wc -l hello.c 2. lcc hello.c; a.out | wc 3. wc 4. ls | wc -w 5. echo wc hello.c | wc -w 6. rm files; ls > files 7. ls | wc -w >> files 8. When you type the command "wc | wc", then ctrl-D, you get the following result: % wc | wc 1 3 25 Why? . . . . ANSWERS TO UNIX SHELL EXERCISES 1. 5, the number of lines in the program. 2. The semicolon delimits two commands; the output is the word count of the output of the program: "hello, world\n". % lcc hello.c; a.out | wc 1 2 13 3. Nothing, it waits for you to type something ("standard input"). If you type ctrl-C, you get your prompt back; if you type ctrl-D, it counts 0 lines, 0 words, and 0 characters: % wc 0 0 0 4. It prints the number of files (and directories) in the current directory. The "|" means that the output of "ls" is "piped" to be taken as the input of "wc -w". The flag "-w" means to count the number of words (as opposed to characters and lines) in the input. 5. 2. The "echo" command puts its argument string into standard input. The string "wc hello.c" has two words. 6. Makes a file named "files" and puts a list of the files (and directories) in the current directory into that file. The "rm files" command deletes an old copy of "files" if there was one. Some Unix environments try to protect you against accidently overwriting old files by not allowing you to use ">" for an existing file. 7. Appends a count of the number of files to the file named files. 8. The output of the first "wc" is given in 3. above. It has 1 line, 3 "words" (the 0s), and 25 characters (24 blanks, the 0s, and a newline).