About Standard Input and Output

About Standard Input and Output

Normally, the output from command lines shows up on your screen. You type in a command and press , and the resulting output appears. This is actually a special case of the more general-purpose way that Unix handles both input and output.

All Unix commands come with two input/output devices, called stdin (for standard input ) and stdout (for standard output ). You can't see the stdin and stdout devices the same way you see a printer, but they are always there. You might like to think of stdin and stdout as valves or hose connectors stuck on the outside of every command. Think of the stdout connector as being fed to your screen, and your keyboard feeding data to the stdin connector.

You have seen redirection of stdout with the > and >> operators earlier in this chapter. In this section, you will learn more about redirecting stdout , and also how to redirect stdin that is, to have a command get input from someplace other than the keyboardand how to connect the stdout of one command to the stdin of another, creating what is called a pipeline . This ability to connect several commands together is one of the most important features of Unix's flexibility.

Besides stdin and stdout , there is one more virtual connection on each command, stderr (for standard error ), which is the output connection for warning and error messages. If a command issues an error message, it comes out of the stderr connector, which is normally connected to your screen (same as stdout ). If you redirect stdout to a file (with > ), stderr still goes to your screen. You can redirect stderr as well, though.

stdin , stdout , and stderr are often capitalized in Unix manuals and literature. Both upper- and lowercase usage is correct.

Figure 2.33 illustrates the concept of the stdin , stdout , and stderr connectors.

Figure 2.33. Here's how the normal connections of stdin , stdout , and stderr work.

Redirecting stdout

The most common form of redirection is to redirect the output of a command into a file using the > operator, which you saw in examples earlier in this chapter. You will redirect stdout to a file when you want to save the output for later use, such as editing or viewing.

To save output in a file:

  • Add > filename to the command line.

    For example,

    ls /Users > users

    redirects the output into the file named users. If the file does not already exist, it is created. If the file does exist, the contents are irrevocably overwritten .


  • Sometimes you'll want to simply throw away the output of a command without ever seeing it. To do this, redirect the output into the special file called /dev/null . For example,

    noisy_command > /dev/null

    Anything written to /dev/null is simply discarded. It's a good place to send insults and complaints.

Sometimes you will want to add the redirected output to a file instead of overwriting the contents.

To append output to a file:

  • Use >> filename at the end of the commandfor example,

    ls /Users >> users

    redirects the output to the end of the file called users, or creates the file if it did not already exist.

Redirecting stderr

stderr can be redirected as well. One reason to do this is to save errors into a log file. Another reason is to not have warning and error messages cluttering up your screen.

To redirect stderr to a file:

  • 2> filename

    puts the stderr output into the named file. You can use 2>> to append rather than overwrite an existing file.

    You can send both stdout and stderr to the same file with

    command 2>&1 filename

(If you are using the tcsh shell, you cannot redirect stdout and stderr separately, but you can make them both go into the same file using >& .) For example:

command >& filename

Redirecting stdin

Sometimes you'll want a command to get its input from a file you have prepared. For example, you might have prepared the text of an e-mail message and want to feed it into the mail command, or you might have a list of filenames you want to feed into a program.

Most Unix commands allow you to redirect standard input and have it come from a file instead of the keyboard.

To take standard input from a file:

  • Add < filename to the end of the command.

    For example, if you have a file that contains a list of 30 filenames, you might type

    ls -l < list_of_files

    instead of

    ls -l file1 file2 file3

    . . . and so on.

Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net