97 lines
4.8 KiB
HTML
97 lines
4.8 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
|
<!Converted with LaTeX2HTML 95.1 (Fri Jan 20 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
|
<HEAD>
|
|
<TITLE>3.8.2 Redirecting input and output</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value="3.8.2 Redirecting input and output">
|
|
<meta name="keywords" value="gs">
|
|
<meta name="resource-type" value="document">
|
|
<meta name="distribution" value="global">
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html3290 HREF="node122.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3288 HREF="node119.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3282 HREF="node120.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3292 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3293 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3291 HREF="node122.html">3.8.3 Using pipes</A>
|
|
<B>Up:</B> <A NAME=tex2html3289 HREF="node119.html">3.8 UNIX Plumbing</A>
|
|
<B> Previous:</B> <A NAME=tex2html3283 HREF="node120.html">3.8.1 Standard input and </A>
|
|
<BR> <HR> <P>
|
|
<H2><A NAME=SECTION00582000000000000000>3.8.2 Redirecting input and output</A></H2>
|
|
<P>
|
|
<A NAME=3064> </A>
|
|
<A NAME=3065> </A>
|
|
<A NAME=3066> </A>
|
|
<A NAME=3067> </A>
|
|
<A NAME=3166> </A>
|
|
Now, let's say that we wanted to send the output of <tt>sort</tt> to a
|
|
file, to save our shopping list elsewhere. The shell allows us to
|
|
<b>redirect</b> standard output to a filename, using the ``<tt>></tt>''
|
|
symbol. Here's how it works.
|
|
<P><TT> /home/larry/papers# <em>sort <b>></b> shopping-list</em> <BR>
|
|
<em>bananas</em> <BR>
|
|
<em>carrots</em> <BR>
|
|
<em>apples</em> <BR>
|
|
<IMG BORDER=0 ALIGN=BOTTOM ALT="" SRC="img184.gif"> <BR>
|
|
/home/larry/papers#
|
|
<P></TT>
|
|
As you can see, the result of the <tt>sort</tt> command isn't
|
|
displayed, instead it's saved to the file <tt>shopping-list</tt>.
|
|
Let's look at this file.
|
|
<P><TT> /home/larry/papers# <em>cat shopping-list</em> <BR>
|
|
apples <BR>
|
|
bananas <BR>
|
|
carrots <BR>
|
|
/home/larry/papers#
|
|
<P></TT>
|
|
Now we can sort our shopping list, and save it, too!
|
|
But let's suppose that we were storing our unsorted, original shopping
|
|
list in the file <tt>items</tt>. One way of sorting the information
|
|
and saving it to a file would be to give <tt>sort</tt> the name of the
|
|
file to read, in lieu of standard input, and redirect standard output
|
|
as we did above. As so:
|
|
<P><TT> /home/larry/papers# <em>sort items <b>></b> shopping-list</em> <BR>
|
|
/home/larry/papers# <em>cat shopping-list</em> <BR>
|
|
apples <BR>
|
|
bananas <BR>
|
|
carrots <BR>
|
|
/home/larry/papers#
|
|
<P></TT>
|
|
<A NAME=3090> </A>
|
|
<A NAME=3091> </A>
|
|
<A NAME=3167> </A>
|
|
However, there's another way of doing this. Not only can we
|
|
redirect standard output, but we can redirect standard <em>input</em>
|
|
as well, using the ``<tt><</tt>'' symbol.
|
|
<P><TT> /home/larry/papers# <em>sort <b><</b> items</em> <BR>
|
|
apples <BR>
|
|
bananas <BR>
|
|
carrots<BR>
|
|
/home/larry/papers#
|
|
<P></TT>
|
|
Technically, <tt>sort < items</tt> is equivalent to <tt>sort items</tt>, but
|
|
the former allows us to demonstrate the point: <tt>sort < items</tt>
|
|
behaves as if the data in the file <tt>items</tt> was typed to
|
|
standard input. The shell handles the redirection. <tt>sort</tt> wasn't given
|
|
the name of the file (<tt>items</tt>) to read; as far as <tt>sort</tt> is
|
|
concerned, it was still reading from standard input as if you had typed
|
|
the data from your keyboard.
|
|
<P>
|
|
<A NAME=3105> </A>
|
|
This introduces the concept of a <b>filter</b>. A filter is a program
|
|
which reads data from standard input, processes it in some way, and
|
|
sends the processed data to standard output. Using redirection,
|
|
standard input and/or standard output can be referenced from files.
|
|
<tt>sort</tt> is a simple filter: it sorts the incoming data and sends the
|
|
result to standard output. <tt>cat</tt> is even simpler: it doesn't do
|
|
anything with the incoming data, it simply outputs whatever was given to it.
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html3290 HREF="node122.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3288 HREF="node119.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3282 HREF="node120.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3292 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3293 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html3291 HREF="node122.html">3.8.3 Using pipes</A>
|
|
<B>Up:</B> <A NAME=tex2html3289 HREF="node119.html">3.8 UNIX Plumbing</A>
|
|
<B> Previous:</B> <A NAME=tex2html3283 HREF="node120.html">3.8.1 Standard input and </A>
|
|
<BR> <HR> <P>
|
|
<BR> <HR>
|
|
<P><ADDRESS>
|
|
<I>Matt Welsh <BR>
|
|
mdw@sunsite.unc.edu</I>
|
|
</ADDRESS>
|
|
</BODY>
|