Redirection (computing)

Redirection (computing)

In computing, Redirection is a function common to most command-line interpreters such as the various Unix shells which allow standard streams to be redirected to user-specified locations.

Programmatically, it is done with the dup2(2) system call, or its less-flexible but higher-level stdio analogues, freopen(3) and popen(3).

Redirecting standard input and standard output

Redirection is usually implemented by placing certain characters between commands. In this context, the characters are often referred to as "hoinkies" in order to "avoid confusion with other bracket-type operators". [Citation
last = Bryant
first = Randal E.
author-link = Randal Bryant
last2 = David
first2 = O'Hallaron
title = Computer Systems: A Programmer's Perspective
place = Upper Saddle River, NJ
publisher = Pearson Education, Inc.
year = 2003
edition = 2003
pages = 794
url = http://csapp.cs.cmu.edu/
isbn = 0-13-034074-X
] Typically, the syntax of these characters is as follows:

command1 > file1

executes command1, placing the output in file1. Note that this will truncate any existing data in file1. To append output to the end of the file, use the >> operator.

command1 < file1

executes command1, using file1 as the source of input (as opposed to the keyboard).

command1 < infile > outfile

combines the two capabilities: command1 reads from infile and writes to outfile

Piping

Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file:

command1 | command2

executes command1, using its output as the input for command2 (commonly called piping, since the "|" character is known as a "pipe").

This is equivalent to using two redirects and a temporary file:

command1 > tempfilecommand2 < tempfilerm tempfile

A good example for command piping is combining echo with another command to achieve something interactive in a non-interactive shell, e.g.

echo -e "user pass" | ftp localhost

This runs the ftp client with input user, press return, then pass.

Redirecting to and from the standard file handles

In Unix shells derived from the original Bourne shell, the first two actions can be further modified by placing a number (the file descriptor) immediately before the character; this will affect which stream is used for the redirection. The Unix standard I/O streams are:

For example:

command1 2> file1

executes command1, directing the standard error stream to file1.

In shells derived from csh (the C shell), the syntax instead appends the & character to the redirect characters, thus achieving a similar result.

Another useful capability is to redirect one standard file handle to another. The most popular variation is to merge standard error into standard output so error messages can be processed together with (or alternately to) the usual output. Example:

find / -name .profile > results 2>&1

will try to find all files named .profile. Executed without redirection, it will output hits to stdout and errors (e.g. for lack of privilege to traverse protected directories) to stderr. If standard output is directed to file results, error messages appear on the console. To see both hits and error messages in file results, merge stderr (handle 2) into stdout (handle 1) using 2>&1 .

It's possible use 2>&1 before ">" but it doesn't work. In fact, when the interpreter reads 2>&1, it doesn't know yet where standard output is redirected and then standard error isn't merged. If the merged output is to be piped into another program, the file merge sequence 2>&1 must precede the pipe symbol, thus:

find / -name .profile 2>&1 | less

A simplified form of the command:

command > file 2>&1

is:

command &>file

or:

command >&file

Chained pipelines

The redirection and piping tokens can be chained together to create complex commands. For example:

ls | grep '.sh' | sort > shlist

lists the contents of the current directory, where this output is filtered to only contain lines which contain .sh, sort this resultant output lexicographically, and place the final output in shlist. This type of construction is used very commonly in shell scripts and batch files.

Redirect to multiple outputs

The standard command tee can redirect output from a command to several destinations.

ls -lrt | tee xyz

This directs the file list output to both standard output as well as to the file xyz.

References

External links

*
* [http://www.linfo.org/redirection.html Redirection Definition] by The Linux Information Project (LINFO)
* [http://technet.microsoft.com/en-us/library/bb490982(en-us).aspx Redirection in Windows]


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Wikipedia:Reference desk/Computing — The Wikipedia Reference Desk covering the topic of computing. Computing #eee #f5f5f5 #eee #aaa #aaa #aaa #00f #36b #000 #00f computing Wikipedia:Reference de …   Wikipedia

  • Folder redirection — In computing, and specifically in the context of Microsoft Windows operating systems, Microsoft refers to Folder Redirection when automatically re routing I/O to/from standard folders (directories) to use storage elsewhere on a network. [… …   Wikipedia

  • Independent Computing Architecture — (ICA) is a proprietary protocol for an application server system, designed by Citrix Systems. The protocol lays down a specification for passing data between server and clients, but is not bound to any one platform. Practical products conforming… …   Wikipedia

  • Pipeline (Unix) — In Unix like computer operating systems, a pipeline is the original software pipeline : a set of processes chained by their standard streams, so that the output of each process ( stdout ) feeds directly as input ( stdin ) of the next one. Each… …   Wikipedia

  • Redirect — For redirection pages in Wikipedia, see Wikipedia:Redirect. Redirect may refer to: Redirection (computing), a capability of command line interpreters Redirect examination, in law URL redirection, techniques on the World Wide Web for making a web… …   Wikipedia

  • URL shortening — For information about human generated short URLs on Wikipedia, see WP:WP and mw:Manual:Short URL. URL shortening is a technique on the World Wide Web in which a Uniform Resource Locator (URL) may be made substantially shorter in length and still… …   Wikipedia

  • Remote Desktop Services — Developer(s) Microsoft Stable release 7.0 (6.1.7600) / October 27, 2009 …   Wikipedia

  • IBM PCjr — (model 4860) Pictured with Racore Drive II third party add on Manufacturer Teledyne, Lewisburg, Tennessee Release date March 1984 ( …   Wikipedia

  • Command-line interface — Screenshot of a sample Bash session. GNOME Terminal 3, Fedora 15 …   Wikipedia

  • Remote Desktop Protocol — (RDP) is a multi channel protocol that allows a user to connect to a computer running Microsoft Terminal Services. Clients exist for most versions of Windows (including handheld versions), and other operating systems such as Linux, FreeBSD,… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”