LinuxDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


Unix Power Tools
Using Standard Input and Output

by Mike Loukides and Jerry Peek
02/24/2000

There is absolutely no difference between reading data from a file and reading data from a terminal. [1] Likewise, if a program's output consists entirely of alphanumeric characters and punctuation, there is no difference between writing to a file, writing to a terminal, and writing to the input of another program (as in a pipe).

The standard I/O facility provides some simple defaults for managing Input/Output. There are three default I/O streams: standard input, standard output, and standard error. By convention, standard output (abbreviated stdout) consists of all "normal" output from your program, while standard error (stderr) consists of error messages. It is often a convenience to be able to handle error messages and standard output separately. If you don't do anything special, programs will read standard input from your keyboard, and they will send standard output and standard error to your terminal's display.

Standard input (stdin) normally comes from your keyboard. Many programs ignore stdin; you name files directly on their command line -- for instance, the command cat file1 file2 never reads its standard input; it reads the files directly. But, without filenames on the command line, UNIX commands that need input will usually read stdin. Standard input normally comes from your keyboard, but the shell can redirect stdin from a file. This is handy for UNIX commands that can't open files directly -- for instance, mail. To mail a file to joan, use < filename - to tell the shell to attach the file, instead of your keyboard, to mail's standard input:

% mail joan < myfile

The real virtue of standard I/O is that it allows you to redirect input or output away from your terminal to a file. UNIX is file-based. Because terminals and other I/O devices are treated as files, a program doesn't care or even know [2] if it is sending its output to a terminal or to a file. For example, if you want to run the command cat file1 file2, but you want to place the output in file3 rather than sending it to your terminal, give the command:

% cat file1 file2 > file3

This is called redirecting standard output to file3. If you give this command and look at file3 afterward, you will find the contents of file1, followed by file2 - exactly what you would have seen on your screen if you omitted the > file3 modifier.

One of the best-known forms of redirection in UNIX is the pipe. The shell's vertical bar (|) operator makes a pipe. For example, to send both file1 and file2 together in a mail message for joan, type:

% cat file1 file2 | mail joan

The pipe says "connect the standard output of the process at the left (cat) to the standard input of the process at the right (mail)."

Table 1 shows the most common ways of redirecting standard I/O, for both the C shell and the Bourne shell.

Table 1: Common Standard I/O Redirections

Functioncshsh
Send stdout to fileprog > fileprog > file
Send stderr to fileprog 2> file
Send stdout and stderr to fileprog >& fileprog > file 2>&1
Take stdin from fileprog < fileprog < file
Send stdout to end of fileprog >> fileprog >> file
Send stderr to end of fileprog 2>> file
Send stdout and stderr to end of fileprog >>& fileprog >> file 2>&1
Read stdin from keyboard until c prog <<cprog <<c
Pipe stdout to prog2prog | prog2prog | prog2
Pipe stdout and stderr to prog2prog |& prog2prog 2>&1 | prog2

Be aware that:

  • While standard I/O is a basic feature of UNIX, the syntax used to redirect standard I/O depends on the shell you are using. Bourne shell syntax and C shell syntax differ, particularly when you get into the less commonly used features. The Korn shell and bash are the same as the Bourne shell, but with a few twists of their own.

  • You can redirect standard input and standard output in the same command line. For example, to read from the file input and write to the file output, give the command:

    % prog < input > output

    The Bourne shell will let you go further and write stderr to a third file:

    $ prog < input > output 2> errors

  • The C shell doesn't give you an easy way to redirect standard output without redirecting standard error. A simple trick will help you do this. To put standard output and standard error in different files, give a command like:

    % ( prog > output ) >& errors

  • Many implementations of both shells don't care what order the redirections appear in, or even where they appear on the command line. For example, SunOS lets you type < input > output prog. However, clarity is always a virtue that computer users have never appreciated enough. It will be easiest to understand what you are doing if you type the command name first -- then redirect standard input, followed by standard output, followed by standard error.

Of course, programs aren't restricted to standard I/O. They can open other files, define their own special-purpose pipes, and write directly to the terminal. But standard I/O is the glue that allows you to make big programs out of smaller ones, and is therefore a crucial part of the operating system. Most UNIX utilities read their data from standard input and write their output to standard output, allowing you to combine them easily. A program that creates its own special-purpose pipe may be very useful, but it cannot be used in combination with standard utilities.

Some UNIX systems, and utilities such as gawk, support special filenames like /dev/stdin, /dev/stdout, and /dev/stderr. You can use these just as you'd use other files. For instance, to have any ordinary command read from the file afile, then standard input (from the keyboard, for example), then the file bfile:

% somecmd afile /dev/stdin bfile

In the same way, a process can write to its standard output through /dev/stdout and the standard error via /dev/stderr.


[1]

If a program's input consists entirely of alphanumeric characters and punctuation (i.e., ASCII data or international (non-English) characters).

[2]

A program can find out.


Back More Unix Power Tools

 




Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!


Linux Resources
  • Linux Online
  • The Linux FAQ
  • linux.java.net
  • Linux Kernel Archives
  • Kernel Traffic
  • DistroWatch.com


  • Sponsored by: