111 lines
3.6 KiB
HTML
111 lines
3.6 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
|
|
<HTML>
|
|
<HEAD>
|
|
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
|
|
<TITLE>BASH Programming - Introduction HOW-TO: All about redirection</TITLE>
|
|
<LINK HREF="Bash-Prog-Intro-HOWTO-4.html" REL=next>
|
|
<LINK HREF="Bash-Prog-Intro-HOWTO-2.html" REL=previous>
|
|
<LINK HREF="Bash-Prog-Intro-HOWTO.html#toc3" REL=contents>
|
|
</HEAD>
|
|
<BODY>
|
|
<A HREF="Bash-Prog-Intro-HOWTO-4.html">Next</A>
|
|
<A HREF="Bash-Prog-Intro-HOWTO-2.html">Previous</A>
|
|
<A HREF="Bash-Prog-Intro-HOWTO.html#toc3">Contents</A>
|
|
<HR>
|
|
<H2><A NAME="s3">3. All about redirection</A> </H2>
|
|
|
|
<H2><A NAME="ss3.1">3.1 Theory and quick reference</A>
|
|
</H2>
|
|
|
|
<P> There are 3 file descriptors, stdin, stdout and stderr (std=standard).
|
|
<P>
|
|
<P>Basically you can:
|
|
<OL>
|
|
<LI> redirect stdout to a file</LI>
|
|
<LI> redirect stderr to a file</LI>
|
|
<LI> redirect stdout to a stderr </LI>
|
|
<LI> redirect stderr to a stdout </LI>
|
|
<LI> redirect stderr and stdout to a file </LI>
|
|
<LI> redirect stderr and stdout to stdout </LI>
|
|
<LI> redirect stderr and stdout to stderr</LI>
|
|
</OL>
|
|
|
|
1 'represents' stdout and 2 stderr.
|
|
<P> A little note for seeing this things: with the less command you can view both stdout
|
|
(which will remain on the buffer) and the stderr that will be printed on the screen, but erased as
|
|
you try to 'browse' the buffer.
|
|
<H2><A NAME="ss3.2">3.2 Sample: stdout 2 file </A>
|
|
</H2>
|
|
|
|
<P> This will cause the ouput of a program to be written to a file.
|
|
<BLOCKQUOTE><CODE>
|
|
<PRE>
|
|
ls -l > ls-l.txt
|
|
|
|
</PRE>
|
|
</CODE></BLOCKQUOTE>
|
|
|
|
Here, a file called 'ls-l.txt' will be created and it will contain what you would see on the
|
|
screen if you type the command 'ls -l' and execute it.
|
|
<H2><A NAME="ss3.3">3.3 Sample: stderr 2 file </A>
|
|
</H2>
|
|
|
|
<P> This will cause the stderr ouput of a program to be written to a file.
|
|
<BLOCKQUOTE><CODE>
|
|
<PRE>
|
|
grep da * 2> grep-errors.txt
|
|
|
|
</PRE>
|
|
</CODE></BLOCKQUOTE>
|
|
|
|
Here, a file called 'grep-errors.txt' will be created and it will contain what you would see
|
|
the stderr portion of the output of the 'grep da *' command.
|
|
<H2><A NAME="ss3.4">3.4 Sample: stdout 2 stderr</A>
|
|
</H2>
|
|
|
|
<P> This will cause the stderr ouput of a program to be written to the same filedescriptor
|
|
than stdout.
|
|
<BLOCKQUOTE><CODE>
|
|
<PRE>
|
|
grep da * 1>&2
|
|
|
|
</PRE>
|
|
</CODE></BLOCKQUOTE>
|
|
|
|
Here, the stdout portion of the command is sent to stderr, you may notice that in differen ways.
|
|
<H2><A NAME="ss3.5">3.5 Sample: stderr 2 stdout </A>
|
|
</H2>
|
|
|
|
<P> This will cause the stderr ouput of a program to be written to the same filedescriptor
|
|
than stdout.
|
|
<BLOCKQUOTE><CODE>
|
|
<PRE>
|
|
grep * 2>&1
|
|
|
|
</PRE>
|
|
</CODE></BLOCKQUOTE>
|
|
|
|
Here, the stderr portion of the command is sent to stdout, if you pipe to less, you'll see that
|
|
lines that normally 'dissapear' (as they are written to stderr) are being kept now (because
|
|
they're on stdout).
|
|
<H2><A NAME="ss3.6">3.6 Sample: stderr and stdout 2 file </A>
|
|
</H2>
|
|
|
|
<P> This will place every output of a program to a file. This is suitable sometimes
|
|
for cron entries, if you want a command to pass in absolute silence.
|
|
<BLOCKQUOTE><CODE>
|
|
<PRE>
|
|
rm -f $(find / -name core) &> /dev/null
|
|
|
|
</PRE>
|
|
</CODE></BLOCKQUOTE>
|
|
|
|
This (thinking on the cron entry) will delete every file called 'core' in any directory. Notice
|
|
that you should be pretty sure of what a command is doing if you are going to wipe it's output.
|
|
<HR>
|
|
<A HREF="Bash-Prog-Intro-HOWTO-4.html">Next</A>
|
|
<A HREF="Bash-Prog-Intro-HOWTO-2.html">Previous</A>
|
|
<A HREF="Bash-Prog-Intro-HOWTO.html#toc3">Contents</A>
|
|
</BODY>
|
|
</HTML>
|