old-www/LDP/intro-linux/html/sect_05_02.html

462 lines
7.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Advanced redirection features</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="Introduction to Linux"
HREF="index.html"><LINK
REL="UP"
TITLE="I/O redirection"
HREF="chap_05.html"><LINK
REL="PREVIOUS"
TITLE="Simple redirections"
HREF="sect_05_01.html"><LINK
REL="NEXT"
TITLE="Filters"
HREF="sect_05_03.html"></HEAD
><BODY
CLASS="sect1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Introduction to Linux: </TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="sect_05_01.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 5. I/O redirection</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="sect_05_03.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="sect_05_02"
></A
>5.2. Advanced redirection features</H1
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_05_02_01"
></A
>5.2.1. Use of file descriptors</H2
><P
>There are three types of I/O, which each have their own identifier, called a file descriptor:</P
><P
></P
><UL
><LI
><P
>standard input: 0</P
></LI
><LI
><P
>standard output: 1</P
></LI
><LI
><P
>standard error: 2</P
></LI
></UL
><P
>In the following descriptions, if the file descriptor number is omitted, and the first character of the redirection operator is &#60;, the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is &#62;, the redirection refers to the standard output (file descriptor 1).</P
><P
>Some practical examples will make this more clear:</P
><P
><B
CLASS="command"
>ls &#62; <TT
CLASS="filename"
>dirlist</TT
> 2&#62;&#38;1</B
> </P
><P
>will direct both standard output and standard error to the file <TT
CLASS="filename"
>dirlist</TT
>, while the command</P
><P
><B
CLASS="command"
>ls 2&#62;&#38;1 &#62; <TT
CLASS="filename"
>dirlist</TT
></B
> </P
><P
>will only direct standard output to <TT
CLASS="filename"
>dirlist</TT
>. This can be a useful option for programmers.</P
><P
>Things are getting quite complicated here, don't confuse the use of the ampersand here with the use of it in <A
HREF="sect_04_01.html#sect_04_01_02_01"
>Section 4.1.2.1</A
>, where the ampersand is used to run a process in the background. Here, it merely serves as an indication that the number that follows is not a file name, but rather a location that the data stream is pointed to. Also note that the bigger-than sign should not be separated by spaces from the number of the file descriptor. If it would be separated, we would be pointing the output to a file again. The example below demonstrates this:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>[nancy@asus /var/tmp]$ </TT
><B
CLASS="command"
>ls 2&#62; <TT
CLASS="filename"
>tmp</TT
></B
>
<TT
CLASS="prompt"
>[nancy@asus /var/tmp]$ </TT
><B
CLASS="command"
>ls -l <TT
CLASS="filename"
>tmp</TT
></B
>
-rw-rw-r-- 1 nancy nancy 0 Sept 7 12:58 tmp
<TT
CLASS="prompt"
>[nancy@asus /var/tmp]$ </TT
><B
CLASS="command"
>ls 2 &#62; <TT
CLASS="filename"
>tmp</TT
></B
>
ls: 2: No such file or directory
</PRE
></FONT
></TD
></TR
></TABLE
><P
>The first command that <EM
>nancy</EM
> executes is correct (eventhough no errors are generated and thus the file to which standard error is redirected is empty). The second command expects that <TT
CLASS="filename"
>2</TT
> is a file name, which does not exist in this case, so an error is displayed.</P
><P
>All these features are explained in detail in the Bash Info pages.</P
></DIV
><DIV
CLASS="sect2"
><H2
CLASS="sect2"
><A
NAME="sect_05_02_02"
></A
>5.2.2. Examples</H2
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_05_02_02_01"
></A
>5.2.2.1. Analyzing errors</H3
><P
>If your process generates a lot of errors, this is a way to thoroughly examine them:</P
><P
><B
CLASS="command"
>command 2&#62;&#38;1 | less</B
> </P
><P
>This is often used when creating new software using the <B
CLASS="command"
>make</B
> command, such as in:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>andy:~/newsoft&#62;</TT
> <B
CLASS="command"
>make all 2&#62;&#38;1 | less</B
>
--output ommitted--
</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_05_02_02_02"
></A
>5.2.2.2. Separating standard output from standard error</H3
><P
>Constructs like these are often used by programmers, so that output is displayed in one terminal window, and errors in another. Find out which pseudo terminal you are using issuing the <B
CLASS="command"
>tty</B
> command first:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>andy:~/newsoft&#62;</TT
> <B
CLASS="command"
>make all 2&#62; /dev/pts/7</B
>
</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="sect3"
><H3
CLASS="sect3"
><A
NAME="sect_05_02_02_03"
></A
>5.2.2.3. Writing to output and files simultaneously</H3
><P
>You can use the <B
CLASS="command"
>tee</B
> command to copy input to standard output and one or more output files in one move. Using the <TT
CLASS="option"
>-a</TT
> option to <B
CLASS="command"
>tee</B
> results in appending input to the file(s). This command is useful if you want to both see and save output. The <B
CLASS="command"
>&#62;</B
> and <B
CLASS="command"
>&#62;&#62;</B
> operators do not allow to perform both actions simultaneously.</P
><P
>This tool is usually called on through a pipe (<B
CLASS="command"
>|</B
>), as demonstrated in the example below:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="screen"
>&#13;<TT
CLASS="prompt"
>mireille ~/test&#62;</TT
> <B
CLASS="command"
>date | tee <TT
CLASS="filename"
>file1 file2</TT
></B
>
Thu Jun 10 11:10:34 CEST 2004
<TT
CLASS="prompt"
>mireille ~/test&#62;</TT
> <B
CLASS="command"
>cat <TT
CLASS="filename"
>file1</TT
></B
>
Thu Jun 10 11:10:34 CEST 2004
<TT
CLASS="prompt"
>mireille ~/test&#62;</TT
> <B
CLASS="command"
>cat <TT
CLASS="filename"
>file2</TT
></B
>
Thu Jun 10 11:10:34 CEST 2004
<TT
CLASS="prompt"
>mireille ~/test&#62;</TT
> <B
CLASS="command"
>uptime | tee <TT
CLASS="option"
>-a</TT
> <TT
CLASS="filename"
>file2</TT
></B
>
11:10:51 up 21 days, 21:21, 57 users, load average: 0.04, 0.16, 0.26
<TT
CLASS="prompt"
>mireille ~/test&#62;</TT
> <B
CLASS="command"
>cat <TT
CLASS="filename"
>file2</TT
></B
>
Thu Jun 10 11:10:34 CEST 2004
11:10:51 up 21 days, 21:21, 57 users, load average: 0.04, 0.16, 0.26
</PRE
></FONT
></TD
></TR
></TABLE
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sect_05_01.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sect_05_03.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Simple redirections</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="chap_05.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Filters</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>