home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


sed & awk

sed & awkSearch this book
Previous: 10.1 The getline Function Chapter 10
The Bottom Drawer
Next: 10.3 The system() Function
 

10.2 The close() Function

The close() function allows you to close open files and pipes. There are a number of reasons you should use it.

  • You can only have so many pipes open at a time. (See the section "Limitations" below, which describes how such limitations can differ from system to system.) In order to open as many pipes in a program as you wish, you must use the close() function to close a pipe when you are done with it (ordinarily, when getline returns 0 or -1). It takes a single argument, the same expression used to create the pipe. Here's an example:

    close("who")

  • Closing a pipe allows you to run the same command twice. For example, you can use date twice to time a command.

  • Using close() may be necessary in order to get an output pipe to finish its work. For example:

    { 
    some processing of
     $0 | "sort > tmpfile" }
    END {
    	close("sort > tmpfile")
    	while ((getline < "tmpfile") > 0) {
    		
    do more work
    
    	}
    }

  • Closing open files is necessary to keep you from exceeding your system's limit on simultaneously open files.

We will see an example of the close() function in the section "Working with Multiple Files" later in this chapter.


Previous: 10.1 The getline Function sed & awk Next: 10.3 The system() Function
10.1 The getline Function Book Index 10.3 The system() Function

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