Go to the first, previous, next, last section, table of contents.
The following functions relate to Input/Output (I/O). Optional parameters are enclosed in square brackets ([ and ]):
close(filename [, how])
close. This second argument
should be one of the two string values "to" or "from",
indicating which end of the pipe to close. Case in the string does
not matter.
See section Two-Way Communications with Another Process,
which discusses this feature in more detail and gives an example.
fflush([filename])
fflush function---@command{gawk} also
buffers its output and the fflush function forces
@command{gawk} to flush its buffers.
fflush was added to the Bell Laboratories research
version of @command{awk} in 1994; it is not part of the POSIX standard and is
not available if @option{--posix} has been specified on the
command line (see section Command-Line Options).
@command{gawk} extends the fflush function in two ways. The first
is to allow no argument at all. In this case, the buffer for the
standard output is flushed. The second is to allow the null string
("") as the argument. In this case, the buffers for
all open output files and pipes are flushed.
fflush returns zero if the buffer is successfully flushed;
otherwise it returns -1.
In the case where all buffers are flushed, the return value is zero
only if all buffers were flushed successfully. Otherwise, it is
-1, and @command{gawk} warns about the filename that had the problem.
@command{gawk} also issues a warning message if you attempt to flush
a file or pipe that was opened for reading (such as with getline),
or if filename is not an open file, pipe, or coprocess.
In such a case, fflush returns -1 as well.
system(command)
system function allows the user to execute operating system
commands and then return to the @command{awk} program. The system
function executes the command given by the string command.
It returns the status returned by the command that was executed as
its value.
For example, if the following fragment of code is put in your @command{awk}
program:
END {
system("date | mail -s 'awk run done' root")
}
the system administrator is sent mail when the @command{awk} program
finishes processing input and begins its end-of-input processing.
Note that redirecting print or printf into a pipe is often
enough to accomplish your task. If you need to run many commands, it
is more efficient to simply print them down a pipeline to the shell:
while (more stuff to do)
print command | "/bin/sh"
close("/bin/sh")
However, if your @command{awk}
program is interactive, system is useful for cranking up large
self-contained programs, such as a shell or an editor.
Some operating systems cannot implement the system function.
system causes a fatal error if it is not supported.
As a side point, buffering issues can be even more confusing, depending upon whether your program is interactive; i.e., communicating with a user sitting at a keyboard.(33)
Interactive programs generally line buffer their output; i.e., they write out every line. Non-interactive programs wait until they have a full buffer, which may be many lines of output. Here is an example of the difference:
$ awk '{ print $1 + $2 }'
1 1
-| 2
2 3
-| 5
Ctrl-d
Each line of output is printed immediately. Compare that behavior with this example:
$ awk '{ print $1 + $2 }' | cat
1 1
2 3
Ctrl-d
-| 2
-| 5
Here, no output is printed until after the Ctrl-d is typed, because it is all buffered and sent down the pipe to @command{cat} in one shot.
system
The fflush function provides explicit control over output buffering for
individual files and pipes. However, its use is not portable to many other
@command{awk} implementations. An alternative method to flush output
buffers is to call system with a null string as its argument:
system("") # flush output
@command{gawk} treats this use of the system function as a special
case and is smart enough not to run a shell (or other command
interpreter) with the empty command. Therefore, with @command{gawk}, this
idiom is not only useful, it is also efficient. While this method should work
with other @command{awk} implementations, it does not necessarily avoid
starting an unnecessary shell. (Other implementations may only
flush the buffer associated with the standard output and not necessarily
all buffered output.)
If you think about what a programmer expects, it makes sense that
system should flush any pending output. The following program:
BEGIN {
print "first print"
system("echo system echo")
print "second print"
}
must print:
first print system echo second print
and not:
system echo first print second print
If @command{awk} did not flush its buffers before calling system, the
latter (undesirable) output is what you see.
Go to the first, previous, next, last section, table of contents.