LinuxBasics.org

The community that helps people to run Linux

rss
Table of Contents

5.2. Advanced redirection features

5.2.1. Use of file descriptors

5.2.1. Introduction of basic concepts

5.2.1.1 Flow of events

When we type commands like ls -l and look at the result on the screen, we tend to see this as a linear event:

input ----command---- output.

In reality, there are 2 possible outcomes to this, usually displayed together on the screen. It gives us the impression of one output, when in fact the real “event” is more like this:

                      / output          \
input  ----command ---                   ---- result. 
                      \ error message   /

So in fact, we could manipulate those two “outcomes” in order for the result to be written differently after the command has been applied (sending one to a file, for instance, and the other to the screen or another file). We can start redirecting these results to different places to suit various purposes by using redirection operators. This feature is very useful for Linux users and administrators, and is the object of this chapter.

Getting back to the command line, the input of a command or action is called standard input, and can be keyboard input, result of a previous command, etc. The output is called standard output. And the error messages are called standard error.

These have been assigned numbers for use in Bash commands: 0 for standard input, 1 for standard output, and 2 for standard error. These are also referred to as stdin, stdout and stderr in C programming.

“The >& is called a redirection operator. x >&y tells the shell to write pipe x into pipe y. Redirection is specified from right to left on the command-line.” Quote from the Rute book, 8.5 Redirecting Streams with >&

In order for Bash to differentiate if you are talking about the number 1 (or 2), or about the descriptor of the standard output or the standard error, you omit the space between the descriptor and the operator (<, >& or >) .

Finally, it is allowed to omit explicitly mentioning if you are dealing with standard input or standard output. By convention, Bash reads the operator as such:

standard input < standard output
standard output > standard input

(i.e. on the narrow (and pointy) side of the operator < is the standard input, whereas on the wide part is the standard output).

5.2.1.2 Putting together those descriptors and operators

As we have seen, there are three types of I/O, which each have their own identifier, called a file descriptor:

In the following descriptions, if the file descriptor number is omitted, and the first character of the redirection operator is <, the redirection refers to the standard input (file descriptor 0). If the first character of the redirection operator is >, the redirection refers to the standard output (file descriptor 1).

Some practical examples will make this more clear:

ls > dirlist 2>&1

will direct both standard output and standard error to the file dirlist.

On the other hand, the command

ls 2>&1 > dirlist

will only direct standard output to dirlist. This can be a useful option for programmers.

In this case, the breakdown of the command goes like this:
> dirlist instructs the system to redirect to the file dirlist what comes before;
before is the expression 2>&1, which says to redirect the standard error to the standard output;
and, finally, the expression ls 2>&… instructs to redirect the standard error of the command ls into, in this case, 1, the standard output (which is then written into the dirlist file).

Notice that nowhere in this command have we told Bash what to do with the original standard output of the ls command, as we specified 2 directly before the operator. If there was no error in the command, we would end up with an empty file.

Things are getting quite complicated here, don’t confuse the use of the ampersand here with the use of it in Section 4.1.2.1, 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 greater-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:

[nancy@asus /var/tmp]$ ls 2> tmp

[nancy@asus /var/tmp]$ ls -l tmp
-rw-rw-r--  1 nancy nancy 0 Sept  7 12:58 tmp

[nancy@asus /var/tmp]$ ls 2 > tmp
ls: 2: No such file or directory

The first command that nancy executes is correct (although no errors are generated and thus the file to which standard error is redirected is empty). The second command expects that 2 is a file name, which does not exist in this case, so an error is displayed.

All these features are explained in detail in the Bash Info pages.

5.2.2. Examples

5.2.2.1. Analyzing errors

If your process generates a lot of errors, this is a way to thoroughly examine them:

command 2>&1 | less

This is often used when creating new software using the make command, such as in:

andy:~/newsoft> make all 2>&1 | less
--output ommitted-- 

5.2.2.2. Separating standard output from standard error

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 tty command first:

andy:~/newsoft> make all 2> /dev/pts/7

5.2.2.3. Writing to output and files simultaneously

You can use the tee command to copy input to standard output and one or more output files in one move. Using the -a option to tee results in appending input to the file(s). This command is useful if you want to both see and save output. The > and >> operators do not allow to perform both actions simultaneously.

This tool is usually called on through a pipe (|), as demonstrated in the example below:

mireille ~/test> date | tee file1 file2
Thu Jun 10 11:10:34 CEST 2004

mireille ~/test> cat file1
Thu Jun 10 11:10:34 CEST 2004

mireille ~/test> cat file2
Thu Jun 10 11:10:34 CEST 2004

mireille ~/test> uptime | tee -a file2
 11:10:51 up 21 days, 21:21, 57 users,  load average: 0.04, 0.16, 0.26

mireille ~/test> cat file2
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
 

New version: New version from updated Garrels book


Copyright (c) by the authors.
Prior to editing, authors agreed to license their contributions by the terms of the GPL.
See our licensing page for details.


Linux® is a registered trademark of Linus Torvalds.


 
  historical/sect_05_02-orig.txt · Last modified: 2008/07/20 19:08

LinuxBasics.org

Start Linux-Course Tutorials Linux Links Security Blog Forum E-mail List Search Online Chat

Site-Info

Help Get in Touch Making of LBo

Wiki-Control

Powered by

Linux Apache DokuWiki Mailman RUTE ht://Dig