rearranged in more sect1 sections.

This commit is contained in:
tille 2005-09-07 13:39:25 +00:00
parent 7835383be9
commit e28e1fcbb5
1 changed files with 52 additions and 32 deletions

View File

@ -18,14 +18,15 @@
</itemizedlist>
</para>
</abstract>
<sect1 id="sect_05_01"><title>What are standard input and standard output?</title>
<sect1 id="sect_05_01"><title>Simple redirections</title>
<sect2 id="sect_05_01_01"><title>What are standard input and standard output?</title>
<para>
Most Linux commands read input, such as a file or another attribute for the command, and write output. By default, input is being given with the keyboard, and output is displayed on your screen. Your keyboard is your <quote>standard input</quote> (stdin) device, and the screen or a particular terminal window is the <quote>standard output</quote> (stdout) device.
</para>
<para>However, since Linux is a flexible system, these default settings don't necessarily have to be applied. The standard output, for example, on a heavily monitored server in a large environment may be a printer.</para>
<sect2 id="sect_05_01_01"><title>The redirection operators</title>
<sect3 id="sect_05_01_01_01"><title>Output redirection with &gt; and |</title>
</sect2>
<sect2 id="sect_05_01_02"><title>The redirection operators</title>
<sect3 id="sect_05_01_02_01"><title>Output redirection with &gt; and |</title>
<para>Sometimes you will want to put output of a command in a file, or you may want to issue another command on the output of one command. This is known as redirecting output. Redirection is done using either the <quote>&gt;</quote> (greater-than symbol), or using the <quote>|</quote> (pipe) operator which sends the standard output of one command to another command as standard input.</para>
<para>As we saw before, the <command>cat</command> command concatenates files and puts them all together to the standard output. By redirecting this output to a file, this file name will be created - or overwritten if it already exists, so take care.</para>
<screen>
@ -76,7 +77,7 @@ ls: newlist: No such file or directory
<para>To find a file in a directory:</para>
<cmdsynopsis><command>ls <option>-l</option> | grep <parameter>part_of_file_name</parameter></command></cmdsynopsis>
</sect3>
<sect3 id="sect_05_01_01_02"><title>Input redirection</title>
<sect3 id="sect_05_01_02_02"><title>Input redirection</title>
<para>In another case, you may want a file to be the input for a command that normally wouldn't accept a file as an option. This redirecting of input is done using the <quote>&lt;</quote> (less-than symbol) operator.</para>
<para>Below is an example of sending a file to somebody, using input redirection.</para>
<screen>
@ -85,7 +86,7 @@ ls: newlist: No such file or directory
<para>If the user <emphasis>mike</emphasis> exists on the system, you don't need to type the full address. If you want to reach somebody on the Internet, enter the fully qualified address as an argument to <command>mail</command>.</para>
<para>This reads a bit more difficult than the beginner's <command>cat <filename>file</filename> | mail <parameter>someone</parameter></command>, but it is of course a much more elegant way of using the available tools.</para>
</sect3>
<sect3 id="sect_05_01_01_03"><title>Combining redirections</title>
<sect3 id="sect_05_01_02_03"><title>Combining redirections</title>
<para>The following example combines input and output redirection. The file <filename>text.txt</filename> is first checked for spelling mistakes, and the output is redirected to an error log file:</para>
<para><command>spell &lt; <filename>text.txt</filename> &gt; <filename>error.log</filename></command></para>
<para>The following command lists all commands that you can issue to examine another file when using <command>less</command>:</para>
@ -113,7 +114,7 @@ ls: newlist: No such file or directory
</sect3>
<sect3 id="sect_05_01_01_04"><title>The &gt;&gt; operator</title>
<sect3 id="sect_05_01_02_04"><title>The &gt;&gt; operator</title>
<para>Instead of overwriting file data, you can also append text to an existing file using two subsequent greater-than signs:</para>
<para>Example:</para>
<screen>
@ -132,9 +133,10 @@ Thu Feb 28 20:23:07 CET 2002
</para>
</sect3>
</sect2>
</sect1>
<sect2 id="sect_05_01_02"><title>Advanced redirection features</title>
<sect3 id="sect_05_01_02_01"><title>Use of file descriptors</title>
<sect1 id="sect_05_02"><title>Advanced redirection features</title>
<sect2 id="sect_05_02_01"><title>Use of file descriptors</title>
<para>There are three types of I/O, which each have their own identifier, called a file descriptor:</para>
<itemizedlist>
<listitem><para>standard input: 0</para></listitem>
@ -153,19 +155,17 @@ Thu Feb 28 20:23:07 CET 2002
<screen>
<prompt>[nancy@asus /var/tmp]$ </prompt><command>ls 2> <filename>tmp</filename></command>
<prompt>[nancy@asus /var/tmp]$ </prompt><command>ls -l <filename>tmp</filename><
/command>
<prompt>[nancy@asus /var/tmp]$ </prompt><command>ls -l <filename>tmp</filename></command>
-rw-rw-r-- 1 nancy nancy 0 Sept 7 12:58 tmp
<prompt>[nancy@asus /var/tmp]$ </prompt><command>ls 2 > <filename>tmp</filename><
/command>
<prompt>[nancy@asus /var/tmp]$ </prompt><command>ls 2 > <filename>tmp</filename></command>
ls: 2: No such file or directory
</screen>
<para>The first command that <emphasis>nancy</emphasis> 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 <filename>2</filename> is a file name, which does not exist in this case, so an error is displayed.</para>
<para>All these features are explained in detail in the Bash Info pages.</para>
</sect3>
<sect3 id="sect_05_01_02_02"><title>Examples</title>
<sect4><title>Analyzing errors</title>
</sect2>
<sect2 id="sect_05_02_02"><title>Examples</title>
<sect3 id="sect_05_02_02_01"><title>Analyzing errors</title>
<para>If your process generates a lot of errors, this is a way to thoroughly examine them:</para>
<cmdsynopsis><command>command 2&gt;&amp;1 | less</command></cmdsynopsis>
<para>This is often used when creating new software using the <command>make</command> command, such as in:</para>
@ -173,16 +173,15 @@ ls: 2: No such file or directory
<prompt>andy:~/newsoft&gt;</prompt> <command>make all 2&gt;&amp;1 | less</command>
--output ommitted--
</screen>
</sect4>
<sect4><title>Separating standard output from standard error</title>
</sect3>
<sect3 id="sect_05_02_02_02"><title>Separating standard output from standard error</title>
<para>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 <command>tty</command> command first:</para>
<screen>
<prompt>andy:~/newsoft&gt;</prompt> <command>make all 2&gt; /dev/pts/7</command>
</screen>
</sect4>
</sect3>
<sect3><title>Writing to output and files simultaneously</title>
<sect3 id="sect_05_02_02_03"><title>Writing to output and files simultaneously</title>
<para>You can use the <command>tee</command> command to copy input to standard output and one or more output files in one move. Using the <option>-a</option> option to <command>tee</command> results in appending input to the file(s). This command is useful if you want to both see and save output. The <command>&gt;</command> and <command>&gt;&gt;</command> operators do not allow to perform both actions simultaneously.</para>
<para>This tool is usually called on through a pipe (<command>|</command>), as demonstrated in the example below:</para>
<screen>
@ -207,10 +206,11 @@ Thu Jun 10 11:10:34 CEST 2004
</sect3>
</sect2>
<sect2 id="sect_05_01_03">
</sect1>
<sect1 id="sect_05_03">
<title>Filters</title>
<para>When a program performs operations on input and writes the result to the standard output, it is called a filter. One of the most common uses of filters is to restructure output. We'll discuss a couple of the most important filters below.</para>
<sect3 id="sect_05_01_03_01"><title>More about grep</title>
<sect2 id="sect_05_03_01"><title>More about grep</title>
<para>As we saw in <xref linkend="sect_03_03_03_04" />, <command>grep</command> scans the output line per line, searching for matching patterns. All lines containing the pattern will be printed to standard output. This behavior can be reversed using the <option>-v</option> option.
</para>
<para>Some examples: suppose we want to know which files in a certain directory have been modified in February:</para>
@ -219,10 +219,9 @@ Thu Jun 10 11:10:34 CEST 2004
</screen>
<para>The <command>grep</command> command, like most commands, is case sensitive. Use the <option>-i</option> option to make no difference between upper and lower case. A lot of GNU extensions are available as well, such as <option>--colour</option>, which is helpful to highlight searchterms in long lines, and <option>--after-context</option>, which prints the number of lines after the last matching line. You can issue a recursive <command>grep</command> that searches all subdirectories of encountered directories using the <option>-r</option> option. As usual, options can be combined.</para>
<para>Regular expressions can be used to further detail the exact character matches you want to select out of all the input lines. The best way to start with regular expressions is indeed to read the <command>grep</command> documentation. An excellent chapter is included in the <command>info <parameter>grep</parameter></command> page. Since it would lead us too far discussing the ins and outs of regular expressions, it is strongly advised to start here if you want to know more about them.</para>
<para>Play around a bit with <command>grep</command>, it will be worth the trouble putting some time in this most basic but very powerful filtering command. The exercises at the end of this chapter will help you to get started, see <xref linkend="sect_05_03" />.</para>
</sect3>
<sect3 id="sect_05_01_03_02"><title>Filtering output</title>
<para>Play around a bit with <command>grep</command>, it will be worth the trouble putting some time in this most basic but very powerful filtering command. The exercises at the end of this chapter will help you to get started, see <xref linkend="sect_05_05" />.</para>
</sect2>
<sect2 id="sect_05_03_02"><title>Filtering output</title>
<para>The command <command>sort</command> arranges lines in alphabetical order by default:</para>
<screen>
<prompt>thomas:~&gt;</prompt> <command>cat people-I-like | sort</command>
@ -237,17 +236,38 @@ My boss
<cmdsynopsis><command>ls <option>-la</option> | sort <option>-nk</option> <parameter>5</parameter></command></cmdsynopsis>
<note><title>Old sort syntax</title>
<para>You might obtain the same result with <command>ls <option>-la</option> | sort <option>+4n</option></command>, but this is an old form which does not comply with the current standards.</para></note>
<para>The <command>sort</command> command is also used in combination with the <command>uniq</command> program (or <command>sort <option>-u</option></command>) to sort output and filter out double entries.</para>
</sect3>
</sect2>
<para>The <command>sort</command> command is also used in combination with the <command>uniq</command> program (or <command>sort <option>-u</option></command>) to sort output and filter out double entries:</para>
<screen>
<prompt>thomas:~> </prompt><command>cat <filename>itemlist</filename></command>
1
4
2
5
34
567
432
567
34
555
<prompt>thomas:~> </prompt><command>sort <filename>itemlist</filename> | uniq</command>
1
2
34
4
432
5
555
567
</screen>
</sect2>
</sect1>
<sect1 id="sect_05_02"><title>Summary</title>
<sect1 id="sect_05_04"><title>Summary</title>
<para>In this chapter we learned how commands can be linked to each other, and how input from one command can be used as output for another command.</para>
<para>Input/output redirection is a common task on UNIX and Linux machines. This powerful mechanism allows flexible use of the building blocks UNIX is made of.</para>
<para>The most commonly used redirections are <command>&gt;</command> and <command>|</command>.</para>
</sect1>
<sect1 id="sect_05_03"><title>Exercises</title>
<sect1 id="sect_05_05"><title>Exercises</title>
<para>These exercises give more examples on how to combine commands. The main goal is to try and use the <keycap>Enter</keycap> key as little as possible.</para>
<para>All exercises are done using a normal user ID, so as to generate some errors. While you're at it, don't forget to read those man pages!</para>
<itemizedlist>