UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 45.19 A while Loop with Several Loop Control Commands Chapter 45
Shell Programming for the Initiated
Next: 45.21 n>&m: Swap Standard Output and Standard Error
 

45.20 Overview: Open Files and File Descriptors

[This introduction is general and simplified. If you're a technical person who needs a complete and exact description, read a book on UNIX programming. -JP]

UNIX shells let you redirect the input and output of programs with operators like > and |. How does that work? How can you use it better? Here's an overview.

When the UNIX kernel starts any process (38.3)- for example, grep, ls, or a shell - it sets up several places for that process to read from and write to. Figure 45.1 shows that.

Figure 45.1: Open Standard I/O Files with No Command-Line Redirection

Figure 45.1

These places are called open files. The kernel gives each file a number called a file descriptor. But people usually use names for these places instead of the numbers:

By default, as Figure 45.1 shows, the file that's opened for stdin, stdout, and stderr is /dev/tty-a name for your terminal. This makes life easier for users - and programmers, too. The user doesn't have to tell a program where to read or write because the default is your terminal. A programmer doesn't have to open files to read or write from (in many cases); the programs can just read from stdin, write to stdout, and send errors to stderr.

This gets better. When the shell starts a process (when you type a command at a prompt), you can tell the shell what file to "connect to" any of those file descriptors. For example, Figure 45.2 shows what happens when you run grep and make the shell redirect grep's standard output away from the terminal to a file named grepout.

Figure 45.2: Standard Output Redirected to a File

Figure 45.2

Programs can read and write files besides the ones on stdin, stdout, and stderr. For instance, in Figure 45.2, grep opened the file somefile itself - it didn't use any of the standard file descriptors for somefile. A UNIX convention is that if you don't name any files on the command line, a program will read from its standard input. Programs that work that way are called filters (1.30).

All shells can do basic redirection with stdin, stdout, and stderr. But, as you'll see in article 45.21, the Bourne shell also handles file descriptors 3 through 9. That's useful sometimes:

- JP


Previous: 45.19 A while Loop with Several Loop Control Commands UNIX Power ToolsNext: 45.21 n>&m: Swap Standard Output and Standard Error
45.19 A while Loop with Several Loop Control Commands Book Index45.21 n>&m: Swap Standard Output and Standard Error

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System