Harley Hahn's Guide to
|
A Personal Note
Chapters...
Command
INSTRUCTOR |
Exercises and Answers for Chapter 15... Standard I/O, Redirection, and Pipes Review Question #1: Summarize the Unix philosophy. Answer The Unix philosophy: Each program or command should be a tool that does only one thing and does it well. When you need a new tool, it is better to combine existing tools than to write new ones. We sometimes describe this philosophy as: "Small is beautiful" or "Less is more". Review Question #2: In Chapter 10, I gave you three questions to ask yourself each time you learn the syntax for a new program:
• What does the command do?
Similarly, what are the three questions you should ask (and answer) whenever you start to learn a new program? Answer Whenever you learn how to use a new program, do not feel as if you must memorize every detail. Rather, start by answering three questions:
• What can this program do for me?
Review Question #3: Collectively, the term "standard I/O" refers to standard input, standard output, and standard error. Define these three terms. What are their abbreviations? What does it mean to redirect standard I/O? Show how to redirect all three types of standard I/O. Answer Standard input is the default input source for a program. When a user logs in, the shell automatically sets standard input to the keyboard. Thus, by default, most programs read their input from the keyboard. Abbreviation: stdin. Standard output is the default target for general output written by a program. When a user logs in, the shell automatically sets standard output to be the screen. Thus, by default, most programs write their output to the monitor. Abbreviation: stdout. Standard error is the default target for error messages written by a program. When a user logs in, the shell automatically sets standard error to be the monitor. Thus, by default, most programs write their error messages to the monitor. Abbreviation: stderr. To redirect standard I/O means to redefine the source for standard input or the target for either standard output or standard error. To redirect stdin: command < inputfile To redirect stdout: command > outputfile To redirect stderr for the Bourne shell family: command 2> errorfile To redirect both stdin and stderr for the Bourne shell family: command > outputfile 2> errorfile To redirect stderr for the C-Shell family: (command > outputfile) >& errorfile To redirect both stdin and stderr for the C- Shell family: command >& outputfile Review Question #4: What is a pipeline? What metacharacter do you use to separate the components of a pipeline? What program would you use at the end of a pipeline to display output one screenful at a time? Answer A pipeline is an arrangement in which two or more programs process data in sequence, the output of one program becoming the input to the next program. The vertical bar (|) character is used to separate the components of a pipeline. To display output one screenful at a time, you can use the less program at the end of a pipeline. Review Question #5: What program do you use to save a copy of data as it passes through a pipeline? Answer To save a copy of data as it passes through a pipeline, you use the tee program. Applying Your Knowledge #1: Show how to redirect the standard output of the date command to a file named currentdate. Answer date > currentdate Applying Your Knowledge #2: The following pipeline counts the number of userids that are currently logged into the system. (The wc -w command counts words; see Chapter 18.) users | wc -w Without changing the output of the pipeline, modify the command to save a copy of the output of users to a file named userlist. Answer To save a copy of the output of users to a file named userlist: users | tee userlist | wc -w Applying Your Knowledge #3: The password file (/etc/passwd) contains one line for each userid registered with the system. Create a single pipeline to sort the lines of the password file, save them to a file called userids, and then display the number of userids on the system. Answer sort < /etc/passwd | tee userids | wc -l Applying Your Knowledge #4: In the following pipeline, the find command (explained in Chapter 25) searches all the directories under /etc looking for files owned by userid root. The names of all such files are then written to standard output, one per line. The output of find is piped to wc -l to count the lines: find /etc -type f -user root -print | wc -l As find does its work, it will generate various error messages you don't want to see. Your goal is to rewrite the pipeline to throw away the error messages without affecting the rest of the output. Show how to do this for the Bourne Shell family. For extra credit, see if you can devise a way to do it for the C-Shell family. (Hint: Use a subshell within a subshell.) Answer Bourne shell family: find /etc -type f -user root -print 2> /dev/null | wc -l C-Shell family: Within a subshell, run the pipeline in a second subshell whose stdout is redirected to the terminal. Then you can redirect the output of the outer subshell — which, at this point, consists only of the stderr of the inner subshell — to /dev/null . (Not for the faint of heart :-) ( ( find /etc -type f -user root -print | wc -l ) > /dev/tty ) >& /dev/null For Further Thought #1: An important part of the Unix philosophy is that, when you need a new tool, it is better to combine existing tools than to write new ones. What happens when you try to apply this guideline to GUI-based tools? Is that good or bad? Answer The advantage of GUI-based tools is that they allow the user to perform actions that would otherwise not be available with text-based tools or would be prohibitively complex with text-based tools. There are trade-offs however. First GUI-based tools require significantly larger programs and a more elaborate working environment. Second, GUI-based tools do not read and write plain text using standard I/O. Thus, such tools cannot be combined to create new tools. This is a serious limitation. For these reasons, the traditional Unix philosophy cannot be applied to GUI-based tools. For Further Thought #2: With the Bourne shell family, it is simple to redirect standard output and standard error separately. This makes it easy to save or discard error messages selectively. With the C-Shell family, separating the two types of output is much more complex. How important is this? The C-Shell was designed by Bill Joy, a brilliant programmer in his day. Why do you think he created such a complicated system? Answer When you use the C-Shell family (Tcsh, C-Shell), the difficulty separating the two types of output is important for sophisticated users. This is one of the reasons why so many people prefer the Bourne Shell family. Designing the C-Shell in this way was a mistake in judgment on Bill Joy's part. For Further Thought #3: As a general rule, the world of computers changes quickly. Why do you think so many of the basic Unix design principles work so well even though they were created over 30 years ago? Answer There are several reasons: • The first few versions of Unix were developed by a relative handful of people who were very, very smart. • Although hardware changes quickly and software changes relatively quickly, there are fundamental principles of computer science and computer programming that do not change. The original Unix developers had a firm grasp of these principles. • In the 1970s, the Unix user community was small, allowing developers to experiment and to make changes quickly. Unlike today's operating system programmers, the original developers did not have to concern themselves with a large installed base of unsophisticated users or with powerful corporate interests. • The synergistic ideas of flexibility, cooperation, and sharing were built into the system from the very beginning. As a result, anyone with a good idea would have a good chance of having his idea incorporated into the operating system. Exercises: Introduction | Chapter list
© All contents Copyright 2024, Harley Hahn
|