Content deleted Content added
AFC draft (via script) |
→Sources: +1 |
||
(16 intermediate revisions by 5 users not shown) | |||
Line 1:
{{More citations needed|date=April 2024}}
<!-- Note: The following pages were redirects to [[
▲<!-- Note: The following pages were redirects to [[Redirection_(computing)]] before draftification:
*[[Redirection operator]]
*[[Redirection (Unix)]]
Line 8 ⟶ 7:
{{Short description|Form of interprocess communication}}
{{distinguish|URL redirection}}
[[Image:Stdstreams-notitle.svg|thumb|300px|The standard streams for input, output, and error]]
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. The concept of redirection is quite old, dating back to the earliest operating systems (OS).{{cn|date=April 2024}} 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.{{sfn | Feiertag | Organick | 1972 | p=}} However, prior to the introduction of [[UNIX]] OS with its "[[Pipeline (Unix)|pipes]]", redirection in operating systems was hard or even impossible to do.{{sfn | Kernighan | Morgan | 1982 | p=780 | loc=Input/output redirection}}
In [[Unix-like]] operating systems, programs do redirection with the {{mono|[[dup2]](2)}} [[system call]], or its less-flexible but higher-level [[Standard streams|stdio]] analogues, {{mono|[[freopen]](3)}} and {{mono|[[popen]](3)}}.<ref>[https://www.gnu.org/software/libc/manual/html_mono/libc.html The GNU C Library Reference Manual for version 2.38] gnu.org</ref>
==Redirecting standard input and standard output==
Line 50 ⟶ 48:
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 {{mono|command1}}, using its output as the input for {{mono|command2}} (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.<ref>{{Cite web|url=https://lwn.net/Articles/270081/|title=KHB: Synthesis: An Efficient Implementation of Fundamental Operating Systems Services|website=lwn.net}}</ref>
This produces the same end result as using two redirects and a temporary file, as in:
Line 79 ⟶ 77:
==Redirecting to and from the standard file handles==
In [[Unix shell]]s derived from the original [[Bourne shell]], the first two actions can be further modified by placing a number (the [[file descriptor]]) immediately before the [[token (parser)|character]]; this will affect which stream is used for the redirection.<ref>{{Cite web|url=https://www.redhat.com/sysadmin/redirect-shell-command-script-output|title=How to redirect shell command output|first=Roberto|last=Nozaki|date=April 21, 2022|website=www.redhat.com}}</ref> The Unix standard I/O streams are:<ref>{{Cite web|url=https://www.gnu.org/software/bash/manual/html_node/Redirections.html|title=Redirections (Bash Reference Manual)|website=www.gnu.org}}</ref>
{| class="wikitable"
Line 114 ⟶ 112:
==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 {{mono|infile}} in lexicographical order, writes unique lines prefixed by the number of occurrences, sorts the resultant output numerically, and places the final output in {{mono|outfile}}.<ref>{{Cite web|url=https://www.linux.org/threads/piping-and-redirecting-output-in-the-linux-terminal.43745/|title=Piping and Redirecting Output in the Linux Terminal |website=Linux.org}}</ref> This type of construction is used very commonly in [[shell script]]s and [[batch file]]s.
==Redirect to multiple outputs==
Line 125 ⟶ 123:
* [[Process substitution]]
* [[Console redirection]]
== References ==
{{Reflist}}
== Sources ==
* {{cite journal | last=Feiertag | first=R. J. | last2=Organick | first2=E. I. | title=The Multics input/output system | journal=ACM SIGOPS Operating Systems Review | volume=6 | issue=1/2 | date=1972 | issn=0163-5980 | doi=10.1145/850614.850622 | pages=35–38}}
* {{cite journal | last=Kernighan | first=Brian W. | last2=Morgan | first2=Samuel P. | title=The UNIX Operating System: A Model for Software Design | journal=Science | publisher=American Association for the Advancement of Science | volume=215 | issue=4534 | year=1982 | eissn=00368075 | issn=10959203 | jstor=1687467 | pages=779–783 | url=http://www.jstor.org/stable/1687467 | access-date=2024-04-25}}
==External links==
Line 134 ⟶ 139:
{{DEFAULTSORT:Redirection (Computing)}}
[[
[[
[[
[[
[[
|