manpagez: man pages & more
info gawk
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.7.1 Special Files for Standard Descriptors

Running programs conventionally have three input and output streams already available to them for reading and writing. These are known as the standard input, standard output, and standard error output. These streams are, by default, connected to your keyboard and screen, but they are often redirected with the shell, via the ‘<’, ‘<<’, ‘>’, ‘>>’, ‘>&’, and ‘|’ operators. Standard error is typically used for writing error messages; the reason there are two separate streams, standard output and standard error, is so that they can be redirected separately.

In other implementations of awk, the only way to write an error message to standard error in an awk program is as follows:

 
print "Serious error detected!" | "cat 1>&2"

This works by opening a pipeline to a shell command that can access the standard error stream that it inherits from the awk process. This is far from elegant, and it is also inefficient, because it requires a separate process. So people writing awk programs often don’t do this. Instead, they send the error messages to the screen, like this:

 
print "Serious error detected!" > "/dev/tty"

(‘/dev/tty’ is a special file supplied by the operating system that is connected to your keyboard and screen. It represents the “terminal,”(25) which on modern systems is a keyboard and screen, not a serial console.) This usually has the same effect but not always: although the standard error stream is usually the screen, it can be redirected; when that happens, writing to the screen is not correct. In fact, if awk is run from a background job, it may not have a terminal at all. Then opening ‘/dev/tty’ fails.

gawk provides special file names for accessing the three standard streams. (c.e.). It also provides syntax for accessing any other inherited open files. If the file name matches one of these special names when gawk redirects input or output, then it directly uses the stream that the file name stands for. These special file names work for all operating systems that gawk has been ported to, not just those that are POSIX-compliant:

/dev/stdin

The standard input (file descriptor 0).

/dev/stdout

The standard output (file descriptor 1).

/dev/stderr

The standard error output (file descriptor 2).

/dev/fd/N

The file associated with file descriptor N. Such a file must be opened by the program initiating the awk execution (typically the shell). Unless special pains are taken in the shell from which gawk is invoked, only descriptors 0, 1, and 2 are available.

The file names ‘/dev/stdin’, ‘/dev/stdout’, and ‘/dev/stderr’ are aliases for ‘/dev/fd/0’, ‘/dev/fd/1’, and ‘/dev/fd/2’, respectively. However, they are more self-explanatory. The proper way to write an error message in a gawk program is to use ‘/dev/stderr’, like this:

 
print "Serious error detected!" > "/dev/stderr"

Note the use of quotes around the file name. Like any other redirection, the value must be a string. It is a common error to omit the quotes, which leads to confusing results.

Finally, using the close() function on a file name of the form "/dev/fd/N", for file descriptor numbers above two, will actually close the given file descriptor.

The ‘/dev/stdin’, ‘/dev/stdout’, and ‘/dev/stderr’ special files are also recognized internally by several other versions of awk.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2024
Individual documents may contain additional copyright information.