Monday, October 5, 2009

Linux - I/O and Redirection

Linux - I/O and Redirection

Unix was created in the days of text-based computing before graphical user interfaces were common. One of the design philosophies was that users would interact with the system through small programs which performed some simple command or purpose. Dennis Ritchie and Ken Thompson designed these simple programs so they would accept input from the keyboard, a file, or as the output of another program. Thus, there was a need to be able to direct the output of a program to either a file or some other program and to also specify where a program was to get its input.

In *nix, the kernel opens and makes available three files for every program that is executed. These three files are called standard input, standard output, and standard error, usually abbreviated to stdin, stdout, and stderr. By default, stdin is attached to the keyboard, and stdout and stderr to the console window. If a program prints something to stdout, then it will be displayed on the console window. If it reads something from stdin, the program will wait for input from the keyboard.

Stdin and stdout can be changed so that rather than reading input from the keyboard, the program will read input from a file. Traditionally in other operating systems of that era, this was done by changing the source code to read the data from a file rather than from the keyboard. One of the most beautiful things about Unix was that this could be accomplished without changing the source code. Rather, the user would redirect stdin from the keyboard to a data file when invoking the program and the source code would remain unchanged. The same could be done for stdout: rather than sending the output of a program to the console window it could be redirected to a file.

·         stdin-'<' OR '0<'

·         stdout-'>' OR '1>'

·         stderr-'2>'

 

Redirection Example 1

An example may be helpful. The program cat normally reads a file and sends the output to the console window. When invoking cat we can redirect its output to a file rather than the console window,

$ cat file01.c > fooie

The > symbol is used to send cat's output to the file fooie rather than to the console window. Note that we have just made a copy of file01.c.

The following c program reads two integers from stdin and outputs the larger of the two to stdout,

/* larger.c */

#include <stdio.h>;

int main() {

int n1, n2;
fprintf(stdout,"Enter an integer: "); fscanf(stdin,"%d",&n1);
fprintf(stdout,"Enter an integer: "); fscanf(stdin,"%d",&n2);
fprintf("The largest integer was the ");
if (n1 > n2) fprintf(stdout,"first one: %d\n",n1);
else fprintf(stdout,"second one: %d\n",n2);
return 0;

}

Since this program reads its input from stdin and writes its output to stdout, we can redirect its input and output so it reads its input from a file and sends its output to a file. For example, suppose we create a text file named larger.in which has two lines, the first with a 2 and the second with a 4. The following command will execute our program and send its output to larger.out.

$ cat larger.in
2
4
$ larger < larger.in > larger.out
$ cat larger.out
Enter an integer: Enter an integer: The largest integer was the second
one: 4

Redirection Example 2

For a different example, consider that cat normally sends its output to the console window. We can redirect cat's output to a file,

$ cat > larger.in
2
4
(^+D)-ctrl + d
$ cat larger.in
2
4

This is a commonly used quick-and-dirty way to create a small, simple text file without invoking a text editor.

Redirecting Stderr

Stderr can be redirected as well,

$ larger < larger.in > larger.out 2> larger.err

Note that > is really 1>, so the above command is equivalent to,

$ larger 0< larger.in 1> larger.out 2> larger.err
ctrl+d

Stderr is not used in C and C++ programs as much as stdout is. The idea with stderr is that error messages—which are special in that they are not part of the normal program output—can be sent to a different location than the normal output. For example, when running a program, you might send stdout to an output file and stderr to the console window,

$ blobx 2> data.out

Since the normal connection for stderr is to the console window, we only need to redirect stdout to the output data file. Any error messages printed to stderr will appear on the console window.

Sending Output to /dev/null

*nix interfaces to "devices" through files, e.g., the CD-ROM drive, the mouse, etc.

A special device named /dev/null is created at startup. Anything written to /dev/null simply "goes away". So, we can use /dev/null and redirect stdout when, for example, we don't want to see the output of a program on the terminal window,

$ prog
output is displayed on the terminal
$ prog > /dev/null

No output to the console.

Anything written to stdout within the program will be redirected to /dev/null and will not be displayed. Stderr can also be redirected to /dev/null. You can think of /dev/null as a black hole, anything that goes in is never seen again.

 

 

No comments:

Post a Comment