CompTIA Linux+ Powered by Linux Professional Institute Study Guide. Richard Blum
Чтение книги онлайн.

Читать онлайн книгу CompTIA Linux+ Powered by Linux Professional Institute Study Guide - Richard Blum страница 19

СКАЧАТЬ the output of one program as input into another program. These facilities can be great tools to tie together multiple programs.

      Part of the Unix philosophy to which Linux adheres is, whenever possible, to do complex things by combining multiple simple tools. Redirection and pipes help in this task by enabling simple programs to be combined together in chains, each link feeding off the output of the preceding link.

      Exploring File Descriptors

      To begin understanding redirection and pipes, you must first understand the different file descriptors. Linux handles all objects as files. This includes a program's input and output stream. To identify a particular file object, Linux uses file descriptors:

      Standard Input

      Programs accept keyboard input via standard input, abbreviated STDIN. Standard input's file descriptor is 0 (zero). In most cases, this is the data that comes into the computer from a keyboard.

      Standard Output

      Text-mode programs send most data to their users via standard output, abbreviated STDOUT. Standard output is normally displayed on the screen, either in a full-screen text-mode session or in a GUI terminal emulator, such as an xterm. Standard output's file descriptor is 1 (one).

      Standard Error

      Linux provides a second type of output stream, known as standard error, abbreviated STDERR. Standard error's file descriptor is 2 (two). This output stream is intended to carry high-priority information such as error messages. Ordinarily, standard error is sent to the same output device as standard output, so you can't easily tell them apart. You can redirect one independently of the other, though, which can be handy. For instance, you can redirect standard error to a file while leaving standard output going to the screen. This allows you to view the error messages at a later time.

      Internally, programs treat STDIN, STDOUT, and STDERR just like data files – they open them, read from or write to the files, and close them when they're done. This is why the file descriptors are necessary and why they can be used in redirection.

      Redirecting Input and Output

      To redirect input or output, you use operators following the command, including any options it takes. For instance, to redirect the STDOUT of the echo command, you would type something like this:

      The result is that the file path.txt contains the output of the command (in this case, the value of the $PATH environment variable). The operator used to perform this redirection was > and the file descriptor used to redirect STDOUT was 1 (one).

      The cat command allows you to display a file's contents to STDOUT. It is described further in the section “Processing Text Using Filters” later in this chapter.

      A nice feature of redirecting STDOUT is that you do not have to use its file descriptor, only the operator. Here's an example of leaving out the 1 (one) file descriptor, when redirecting STDOUT:

      You can see that even without the STDOUT file descriptor, the output was redirected to a file. However, the redirection operator (>) was still needed.

You can also leave out the STDIN file descriptor when using the appropriate redirection operator. Redirection operators exist to achieve several effects, as summarized in Table 1.2.

Table 1.2 Common redirection operators

      Most of these redirectors deal with output, both because there are two types of output (standard output and standard error) and because you must be concerned with what to do in case you specify a file that already exists. The most important input redirector is <, which takes the specified file's contents as standard input.

      A common trick is to redirect standard output or standard error to /dev/null. This file is a device that's connected to nothing; it's used when you want to get rid of data. For instance, if the whine program is generating too many unimportant error messages, you can type whine 2> /dev/null to run it and discard its error messages.

      One redirection operator that requires elaboration is the << operator. This operator implements something called a here document. A here document takes text from subsequent lines as standard input. Chances are you won't use this redirector on the command line. Subsequent lines are standard input, so there's no need to redirect them. Rather, you might use this command in a script to pass data to an interactive program. Unlike with most redirection operators, the text immediately following the << code isn't a filename; instead, it's a word that's used to mark the end of input. For instance, typing someprog << EOF causes someprog to accept input until it sees a line that contains only the string EOF (without even a space following it).

      Some programs that take input from the command line expect you to terminate input by pressing Ctrl+D. This keystroke corresponds to an end-of-file marker using the American Standard Code for Information Interchange (ASCII).

      Piping Data between Programs

      Programs can frequently operate on other programs' outputs. For instance, you might use a text-filtering command (such as the ones described shortly in “Processing Text Using Filters”) to manipulate text output by another program. You can do this with the help of redirection operators: send the first program's standard output to a file, and then redirect the second program's standard input to read from that file. This method is awkward, though, and it involves the creation of a file that you might easily overlook, leading to unnecessary clutter on your system.

      The solution is to use data pipes (aka pipelines). A pipe redirects the first program's standard output to the second program's standard input, and it is denoted by a vertical bar (|):

      For instance, suppose that first generates some system statistics, such as system uptime, CPU use, number of users logged in, and so on. This output might be lengthy, so you want to trim it a bit. You might therefore use second, which could be a script or command that echoes from its standard input only the information in which you're interested. (The grep command, described in “Using grep,” is often used in this role.)

      Pipes can be used in sequences of arbitrary length:

      Another redirection tool often used with pipes is the tee command. This command splits standard input so that it's displayed on standard output and in as many files as you specify. Typically, tee is used in conjunction with data pipes so that a program's output can be both stored and viewed immediately. For instance, to view and store the output of the echo $PATH command, you might type this:

СКАЧАТЬ