Redirection (computing)
![]() In computing, redirection is a form of interprocess communication, and is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations. The concept of redirection is quite old, dating back to the earliest operating systems (OS).[citation needed] A discussion of the design goals for redirection can be found already in the 1971 description of the input-output subsystem of the Multics OS.[1] However, prior to the introduction of UNIX OS with its "pipes", redirection in operating systems was hard or even impossible to do.[2] In Unix-like operating systems, programs do redirection with the dup2(2) system call, or its less-flexible but higher-level stdio analogues, freopen(3) and popen(3).[3] Redirecting standard input and standard outputRedirection is usually implemented by placing certain characters between commands. BasicTypically, the syntax of these characters is as follows, using Using
VariantsTo append output to the end of the file, rather than clobbering it, the To read from a stream literal (an inline file, passed to the standard input), one can use a here document, using the $ tr a-z A-Z << END_TEXT
> one two three
> uno dos tres
> END_TEXT
ONE TWO THREE
UNO DOS TRES
To read from a string, one can use a here string, using the $ NUMBERS="one two three"
$ tr a-z A-Z <<< "$NUMBERS"
ONE TWO THREE
Piping![]() Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file. The two programs performing the commands may run in parallel with the only storage space being working buffers (Linux allows up to 64K for each buffer) plus whatever work space each command's processing requires. For example, a "sort" command is unable to produce any output until all input records have been read, as the very last record received just might turn out to be first in sorted order. Dr. Alexia Massalin's experimental operating system, Synthesis, would adjust the priority of each task as they ran according to the fullness of their input and output buffers.[4] This produces the same end result as using two redirects and a temporary file, as in: $ command1 > tempfile
$ command2 < tempfile
$ rm tempfile
But here, command2 does not start executing until command1 has finished, and a sufficiently large scratch file is required to hold the intermediate results as well as whatever work space each task required. As an example, although DOS allows the "pipe" syntax, it employs this second approach. Thus, suppose some long-running program "Worker" produces various messages as it works, and that a second program, TimeStamp copies each record from stdin to stdout, prefixed by the system's date and time when the record is received. A sequence such as A good example for command piping is combining In casual use, the initial step of a pipeline is often $ cat infile | command
$ echo $string | command
$ echo -e 'user\npass' | ftp localhost
can be replaced by: $ command < infile
$ command <<< $string
$ ftp localhost <<< $'user\npass'
As Redirecting to and from the standard file handlesIn 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.[5] The Unix standard I/O streams are:[6]
For example, In shells derived from csh (the C shell), the syntax instead appends the & (ampersand) character to the redirect characters, thus achieving a similar result. The reason for this is to distinguish between a file named '1' and stdout, i.e. 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. For example, If the merged output is to be piped into another program, the file merge sequence A simplified but non-POSIX conforming form of the command, It is possible to use In the following example, standard output is written to file, but errors are redirected from stderr to stdout, i.e. sent to the screen: To write both errors and standard output to file, the order should be reversed. Standard output would first be redirected to the file, then stderr would additionally be redirected to the stdout handle that has already been changed to point at the file: Chained pipelinesThe redirection and piping tokens can be chained together to create complex commands. For example, Redirect to multiple outputsThe standard command tee can redirect output from a command to several destinations: See also
References
Sources
External links
|
Portal di Ensiklopedia Dunia