| ★ wanayoo — archive 1999 http://oreilly.linux.com/pub/a/linux/lpt/13_01.html | Nouvelle recherche | Portail wanayoo |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
![]() |
|
|
Sponsored by: |
|||||||||||||||||||||||||||||||||
Unix Power Tools
by Mike Loukides
and Jerry Peek
|
| Function | csh | sh |
| Send stdout to file | prog > file | prog > file |
| Send stderr to file | prog 2> file | |
| Send stdout and stderr to file | prog >& file | prog > file 2>&1 |
| Take stdin from file | prog < file | prog < file |
| Send stdout to end of file | prog >> file | prog >> file |
| Send stderr to end of file | prog 2>> file | |
| Send stdout and stderr to end of file | prog >>& file | prog >> file 2>&1 |
| Read stdin from keyboard until c | prog <<c | prog <<c |
| Pipe stdout to prog2 | prog | prog2 | prog | prog2 |
| Pipe stdout and stderr to prog2 | prog |& prog2 | prog 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 bfileIn 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. |
|
|
|
|
|
|
|
Copyright © 2000 O'Reilly and Associates, Inc. All Rights Reserved. All trademarks and registered trademarks appearing on the O'Reilly Network are the property of their respective owners. |
|
|
|
|