Filehandles


Filehandles are symbolic IDs for files and pseudo-files that are set up each time the kernel starts a process. These numbers are what the process uses to write output and read input. Three filehandles are opened by default:

Filehandle

Meaning

F.H. 0

STDIN. This is where standard input comes from. This is normally the keyboard, but it can be redirected to read from a file or some other source.

F.H. 1

STDOUT. This where standard output goes. It is normally the screen, but as you have seen it can be redirected.

F.H. 2

STDERR. This is where standard error messages go. Once again, this is normally the screen, but it can also be redirected.


Filehandles can be used in shell programs to make your programs both more efficient and easier to write. The exec command can be used to open a filehandle other than one of the default ones so as to redirect your program's output to it.

Suppose you have a program that wants to write a lot of lines to a file. Normally you would have to use output redirection (the > operator) on every single echo or printf command, opening a filehandle and closing it for each command. Listing 10.21 shows an example of how directly addressing filehandles can make your programs both easier to write and more efficient.

Listing 10.21. Using Filehandles to Redirect Output

1.  #!/bin/sh 2.  # open a filehandle on F.H. 1 (STDOUT) 3.  exec > testfile.txt 4.  # echo some stuff to STDOUT which will now go to the file testfile.txt 5.  echo "Line 1 of the file" 6.  echo "Line 2 of the file" 7.  echo "Line 3 of the file" 8.  echo "Line 4 of the file" 9.  echo "line 5 of the file" 10. exit 0

The exec statement in the second line of the preceding code example causes testfile.txt to be opened as a stand-in for STDOUT. As a result, all the echo lines are set to the file testfile.txt instead of to the screen, even though the output is not redirected in the echo statements. If you have a lot of things that need to be written to a file, this is more efficient for the operating system than using shell redirection with every single command, as well as being far easier to code.

This is also very useful when opening filehandles for STDIN and using read. Listing 10.22 shows an example.

Listing 10.22. Using a Filehandle for Input

1.  #!/bin/sh 2.  # open a file descriptor on F.D. 0 (STDIN) 3.  exec < testfile.txt 4.  while read string 5.  do 6.      echo $string 7.  done

Assuming that you still have the file testfile.txt from the previous example, this example will output the following:

Line 1 of the file Line 2 of the file Line 3 of the file Line 4 of the file Line 5 of the file


What's so great about this? It demonstrates the important concept of using a filehandle with read, enabling your script to read a multiline file and act on each line individually. Because the filehandle is not closed between each call to read, read remembers the last line it read and moves the pointer to the next line in the file. This way, read will read each line of the file in sequence automatically.




FreeBSD 6 Unleashed
FreeBSD 6 Unleashed
ISBN: 0672328755
EAN: 2147483647
Year: 2006
Pages: 355
Authors: Brian Tiemann

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net