Redirection (computing): Difference between revisions

Content deleted Content added
Undid revision 913378981 by Astra1999 (talk)
m Replaces obsolete tag.
Line 5:
In [[computing]], '''redirection''' is a form of [[interprocess communication]], and is a function common to most [[command-line interpreter]]s, including the various [[Unix shell]]s that can redirect [[standard streams]] to user-specified locations.
 
In [[Unix-like]] operating systems, programs do redirection with the <tt>{{mono|[[dup2]](2)</tt>}} [[system call]], or its less-flexible but higher-level [[Standard streams|stdio]] analogues, <tt>{{mono|[[freopen]](3)</tt>}} and <tt>{{mono|[[popen]](3)</tt>}}.
 
==Redirecting standard input and standard output==
Line 11:
 
===Basic===
Typically, the [[syntax]] of these characters is as follows, using <code>&lt;</code> to redirect input, and <code>&gt;</code> to redirect output. <syntaxhighlight lang="bash" inline>command1 > file1</syntaxhighlight> executes <tt>{{mono|command1</tt>}}, placing the output in <tt>{{mono|file1</tt>}}, as opposed to displaying it at the terminal, which is the usual destination for standard output. This will [[Clobbering|clobber]] any existing data in <tt>{{mono|file1</tt>}}.
 
Using <syntaxhighlight lang="bash" inline>command1 < file1</syntaxhighlight> executes <tt>{{mono|command1</tt>}}, with <tt>{{mono|file1</tt>}} as the source of input, as opposed to the [[Computer keyboard|keyboard]], which is the usual source for standard input.
 
<syntaxhighlight lang="bash" inline>command1 < infile > outfile</syntaxhighlight> combines the two capabilities: <tt>{{mono|command1</tt>}} reads from <tt>{{mono|infile</tt>}} and writes to <tt>{{mono|outfile</tt>}}
 
===Variants===
Line 37:
==Piping==
[[Image:Pipeline.svg|thumb|A pipeline of three programs run on a text terminal]]
Programs can be run together such that one program reads the output from another with no need for an explicit intermediate file. <syntaxhighlight lang="bash" inline>command1 | command2</syntaxhighlight> executes <tt>{{mono|command1</tt>}}, using its output as the input for <tt>{{mono|command2</tt>}} (commonly called [[Pipeline (Unix)|piping]], with the "<code>|</code>" character being known as the "pipe").
 
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 kernel#Massalin.27s Synthesis kernel|Synthesis]], would adjust the priority of each task as they ran according to the fullness of their input and output buffers.
Line 49:
</syntaxhighlight>
 
But here, <tt>{{mono|command2</tt>}} does not start executing until <tt>{{mono|command1</tt>}} 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 <syntaxhighlight inline lang="bash">Worker | TimeStamp > LogFile.txt</syntaxhighlight> would produce timestamps only when Worker had finished, merely showing how swiftly its output file could be read and written.
 
A good example for command piping is combining <code>[[echo (command)|echo]]</code> with another command to achieve something interactive in a non-interactive shell, e.g. <syntaxhighlight lang="bash" inline>echo -e 'user\npass' | ftp localhost</syntaxhighlight>. This runs the [[File Transfer Protocol|ftp]] client with input <tt>{{mono|user</tt>}}, press <tt>{{mono|return</tt>}}, then <tt>{{mono|pass</tt>}}.
 
In casual use, the initial step of a pipeline is often <code>cat</code> or <code>echo</code>, reading from a file or string. This can often be replaced by input indirection or a [[here string]], and use of cat and piping rather than input redirection is known as [[useless use of cat]]. For example, the following commands:
Line 83:
|}
 
For example, <syntaxhighlight lang="bash" inline>command1 2> file1</syntaxhighlight> executes <tt>{{mono|command1</tt>}}, directing the [[Standard streams#Standard error (stderr)|standard error]] stream to <tt>{{mono|file1</tt>}}.
 
In shells derived from [[C shell|csh]] (the [[C shell]]), the syntax instead appends the <tt>{{mono|&</tt>}} (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. '<syntaxhighlight lang="bash" inline>cat file 2>1'</syntaxhighlight> vs '<syntaxhighlight lang="bash" inline>cat file 2>&1'</syntaxhighlight>. In the first case, stderr is redirected to a file named '{{mono|1}}' and in the second, stderr is redirected to stdout.
 
Another useful capability is to redirect one standard file handle to another. The most popular variation is to merge [[Standard streams#Standard error (stderr)|standard error]] into [[Standard streams#Standard output (stdout)|standard output]] so error messages can be processed together with (or alternately to) the usual output. For example, <syntaxhighlight lang="bash" inline>find / -name .profile > results 2>&1</syntaxhighlight> will try to find all files named <tt>{{mono|.profile</tt>}}. 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 <tt>{{mono|results</tt>}}, error messages appear on the console. To see both hits and error messages in file <tt>{{mono|results</tt>}}, merge [[stderr]] (handle 2) into [[stdout]] (handle 1) using '''<tt>{{code|2>&1</tt>'''}}.
 
If the merged output is to be piped into another program, the file merge sequence '''<tt>{{code|2>&1</tt>'''}} must precede the pipe symbol, thus, <syntaxhighlight lang="bash" inline>find / -name .profile 2>&1 | less</syntaxhighlight>
 
A simplified but non-POSIX conforming form of the command, <syntaxhighlight lang="bash" inline>command > file 2>&1</syntaxhighlight> is (not available in Bourne Shell prior to version 4, final release, or in the standard shell [[Debian Almquist shell]] used in Debian/Ubuntu): <syntaxhighlight lang="bash" inline>command &>file</syntaxhighlight> or <syntaxhighlight lang="bash" inline>command >&file</syntaxhighlight>.
Line 104:
==Chained pipelines==
The redirection and piping tokens can be chained together to create complex commands. For example, <syntaxhighlight lang="bash" inline>sort infile | uniq -c | sort -n > outfile</syntaxhighlight> sorts the lines of <tt>{{mono|infile</tt>}} in lexicographical order, writes unique lines prefixed by the number of occurrences, sorts the resultant output numerically, and places the final output in <tt>{{mono|outfile</tt>}}. This type of construction is used very commonly in [[shell script]]s and [[batch file]]s.
 
==Redirect to multiple outputs==
The standard command <tt>{{mono|[[tee (command)|tee]]</tt>}} can redirect output from a command to several destinations:<syntaxhighlight lang="bash" inline>ls -lrt | tee xyz</syntaxhighlight>. This directs the file list output to both standard output and the file <tt>{{mono|xyz</tt>}}.
 
==See also==