indexterms

This commit is contained in:
tille 2006-10-23 09:47:49 +00:00
parent 11c001a5f3
commit e28938939f
1 changed files with 23 additions and 23 deletions

View File

@ -21,14 +21,14 @@
<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.
Most Linux commands read input<indexterm><primary>input</primary><secondary>default input</secondary></indexterm>, such as a file or another attribute for the command, and write output. By default, input is being given with the keyboard, and output<indexterm><primary>output</primary><secondary>default output</secondary></indexterm> is displayed on your screen. Your keyboard is your<indexterm><primary>input</primary><secondary>standard input</secondary></indexterm> <emphasis>standard input</emphasis> (stdin<indexterm><primary>stdin</primary></indexterm>) device, and the screen or a particular terminal window is the <emphasis>standard output<indexterm><primary>output</primary><secondary>standard output</secondary></indexterm></emphasis> (stdout<indexterm><primary>stdout</primary></indexterm>) 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>
<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>
<para>Sometimes you will want to put output<indexterm><primary>output</primary><secondary>redirection with &gt;</secondary></indexterm> 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<indexterm><primary>greater-than</primary></indexterm>), or using the <quote>|</quote> (pipe<indexterm><primary>pipe</primary></indexterm>) operator which sends the standard output of one command to another command<indexterm><primary>output</primary><secondary>redirection with |</secondary></indexterm> 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<indexterm><primary>I/O redirection</primary><secondary>saving output in a file</secondary></indexterm> will be created - or overwritten if it already exists, so take care.</para>
<screen>
<prompt>nancy:~&gt;</prompt> <command>cat test1</command>
some words
@ -44,10 +44,10 @@ some other words
</screen>
<warning><title>Don't overwrite!</title>
<para>Be careful not to overwrite existing (important) files when redirecting output. Many shells, including Bash, have a built-in feature to protect you from that risk: <command>noclobber</command>. See the Info pages for more information. In Bash, you would want to add the <command>set <option>-o</option> <parameter>noclobber</parameter></command> command to your <filename>.bashrc</filename> configuration file in order to prevent accidental overwriting of files.</para></warning>
<para>Be careful not to overwrite existing (important) files when redirecting output. Many shells, including <application>Bash</application>, have a built-in feature to protect you from that risk: <command>noclobber<indexterm><primary>Bash</primary><secondary>noclobber</secondary></indexterm></command>. See the <application>Info</application> pages for more information. In <application>Bash</application>, you would want to add the <command>set <option>-o</option> <parameter>noclobber<indexterm><primary>noclobber</primary></indexterm></parameter></command> command to your <filename>.bashrc<indexterm><primary>.bashrc</primary></indexterm></filename> configuration file in order to prevent<indexterm><primary>I/O redirection</primary><secondary>prevent overwrite</secondary></indexterm> accidental overwriting of files.</para></warning>
<para>Redirecting <quote>nothing</quote> to an existing file is equal to emptying the file:</para>
<para>Redirecting <quote>nothing</quote> to an existing file is equal to emptying the file<indexterm><primary>I/O redirection</primary><secondary>emptying files</secondary></indexterm>:</para>
<screen>
<prompt>nancy:~&gt;</prompt> <command>ls -l list</command>
-rw-rw-r-- 1 nancy nancy 117 Apr 2 18:09 list
@ -57,7 +57,7 @@ some other words
<prompt>nancy:~&gt;</prompt> <command>ls -l list</command>
-rw-rw-r-- 1 nancy nancy 0 Apr 4 12:01 list
</screen>
<para>This process is called <emphasis>truncating</emphasis>.</para>
<para>This process is called <emphasis>truncating<indexterm><primary>truncating</primary></indexterm></emphasis>.</para>
<para>The same redirection to an nonexistent file will create a new empty file with the given name:</para>
<screen>
<prompt>nancy:~&gt;</prompt> <command>ls -l newlist</command>
@ -69,7 +69,7 @@ ls: newlist: No such file or directory
-rw-rw-r-- 1 nancy nancy 0 Apr 4 12:05 newlist
</screen>
<para><xref linkend="chap_07" /> gives some more examples on the use of this sort of redirection.</para>
<para>Some examples using piping of commands:</para>
<para>Some examples using piping<indexterm><primary>piping</primary><secondary>examples</secondary></indexterm> of commands:</para>
<para>To find a word within some text, display all lines matching <quote>pattern1</quote>, and exclude lines also matching <quote>pattern2</quote> from being displayed:</para>
<cmdsynopsis><command>grep <parameter>pattern1</parameter> <filename>file</filename> | grep <option>-v</option> <parameter>pattern2</parameter></command></cmdsynopsis>
<para>To display output of a directory listing one page at a time:</para>
@ -78,7 +78,7 @@ ls: newlist: No such file or directory
<cmdsynopsis><command>ls <option>-l</option> | grep <parameter>part_of_file_name</parameter></command></cmdsynopsis>
</sect3>
<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>In another case, you may want a file to be the input<indexterm><primary>input</primary><secondary>redirection</secondary></indexterm> 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<indexterm><primary>less-than</primary></indexterm> symbol) operator.</para>
<para>Below is an example of sending a file to somebody, using input redirection.</para>
<screen>
<prompt>andy:~&gt;</prompt> <command>mail mike@somewhere.org &lt; to_do</command>
@ -87,7 +87,7 @@ ls: newlist: No such file or directory
<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_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>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<indexterm><primary>I/O redirection</primary><secondary>combining</secondary></indexterm> 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>
<screen>
@ -108,14 +108,14 @@ ls: newlist: No such file or directory
:p * Examine the (N-th) previous file from the command line.
:x * Examine the first (or N-th) file from the command line.
</screen>
<para>Output of one command can be piped into another command virtually as many times as you want, just as long as these commands would normally read input from standard input and write output to the standard output. Sometimes they don't, but then there may be special options that instruct these commands to behave according to the standard definitions; so read the documentation (man and info pages) of the commands you use if you should encounter errors.</para>
<para>Output of one command can be piped into another command virtually as many times as you want, just as long as these commands would normally read input from standard input and write output to the standard output. Sometimes they don't, but then there may be special options that instruct these commands to behave according to the standard definitions; so read the documentation (man and <application>Info</application> pages) of the commands you use if you should encounter errors.</para>
<para>Again, make sure you don't use names of existing files that you still need. Redirecting output to existing files will replace the content of those files.</para>
</sect3>
<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>Instead of overwriting file data, you can also append text to an existing file using two subsequent<indexterm><primary>I/O redirection</primary><secondary>append output</secondary></indexterm> greater-than signs:</para>
<para>Example:</para>
<screen>
<prompt>mike:~&gt;</prompt> <command>cat <filename>wishlist</filename></command>
@ -137,16 +137,16 @@ Thu Feb 28 20:23:07 CET 2002
<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>
<para>There are three types of I/O, which each have their own identifier, called a file descriptor<indexterm><primary>file descriptors</primary><secondary>overview</secondary></indexterm>:</para>
<itemizedlist>
<listitem><para>standard input: 0</para></listitem>
<listitem><para>standard output: 1</para></listitem>
<listitem><para>standard error: 2</para></listitem>
<listitem><para>standard error<indexterm><primary>standard error</primary></indexterm>: 2</para></listitem>
</itemizedlist>
<para>In the following descriptions, if the file descriptor number is omitted, and the first character of the redirection operator is &lt;, the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is &gt;, the redirection refers to the standard output (file descriptor 1).</para>
<para>Some practical examples will make this more clear:</para>
<para>Some practical examples will make this more<indexterm><primary>file descriptors</primary><secondary>examples</secondary></indexterm> clear:</para>
<cmdsynopsis><command>ls &gt; <filename>dirlist</filename> 2&gt;&amp;1</command></cmdsynopsis>
<para>will direct both standard output and standard error to the file <filename>dirlist</filename>, while the command</para>
<cmdsynopsis><command>ls 2&gt;&amp;1 &gt; <filename>dirlist</filename></command></cmdsynopsis>
@ -167,7 +167,7 @@ ls: 2: No such file or directory
</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>
<para>If your process generates a lot of errors, this is a way to thoroughly examine them<indexterm><primary>file descriptors</primary><secondary>examples</secondary></indexterm>:</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>
<screen>
@ -176,14 +176,14 @@ ls: 2: No such file or directory
</screen>
</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>
<para>Constructs<indexterm><primary>I/O redirection</primary><secondary>splitting stdout and stderr</secondary></indexterm> 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>
</sect3>
<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>You can use the <command>tee<indexterm><primary>tee</primary></indexterm></command> command to copy input to standard<indexterm><primary>I/O redirection</primary><secondary>write to stdout and file</secondary></indexterm> 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>
<prompt>mireille ~/test&gt;</prompt> <command>date | tee <filename>file1 file2</filename></command>
@ -212,18 +212,18 @@ Thu Jun 10 11:10:34 CEST 2004
<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>
<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>As we saw in <xref linkend="sect_03_03_03_04" />, <command>grep<indexterm><primary>grep</primary><secondary>I/O redirection</secondary></indexterm></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>
<para>Some examples: suppose we want to know which files in a certain directory have been modified<indexterm><primary>filtering</primary><secondary>grep</secondary></indexterm> in February:</para>
<screen>
<prompt>jenny:~&gt;</prompt> <command>ls -la | grep Feb</command>
</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>Regular expressions<indexterm><primary>regular expressions</primary><secondary>getting started</secondary></indexterm> 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>grep</command> <application>Info</application> 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_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>
<para>The command <command>sort<indexterm><primary>sort</primary></indexterm></command> arranges lines in alphabetical<indexterm><primary>filtering</primary><secondary>sort</secondary></indexterm> order by default:</para>
<screen>
<prompt>thomas:~&gt;</prompt> <command>cat people-I-like | sort</command>
Auntie Emmy
@ -233,11 +233,11 @@ Grandma
Mum
My boss
</screen>
<para>But there are many more things <command>sort</command> can do. Looking at the file size, for instance. With this command, directory content is sorted smallest files first, biggest files last:</para>
<para>But there are many more things <command>sort</command> can do. Looking at the file size, for instance. With this command, directory content is sorted smallest files<indexterm><primary>files</primary><secondary>size</secondary></indexterm> first, biggest<indexterm><primary>sort</primary><secondary>file size</secondary></indexterm> files last:</para>
<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>
<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<indexterm><primary>sort</primary><secondary>unique</secondary></indexterm> entries:</para>
<screen>
<prompt>thomas:~> </prompt><command>cat <filename>itemlist</filename></command>
1