gawk: Nonfatal
1
1 5.10 Enabling Nonfatal Output
1 =============================
1
1 This minor node describes a 'gawk'-specific feature.
1
1 In standard 'awk', output with 'print' or 'printf' to a nonexistent
1 file, or some other I/O error (such as filling up the disk) is a fatal
1 error.
1
1 $ gawk 'BEGIN { print "hi" > "/no/such/file" }'
1 error-> gawk: cmd. line:1: fatal: can't redirect to `/no/such/file' (No
1 error-> such file or directory)
1
1 'gawk' makes it possible to detect that an error has occurred,
1 allowing you to possibly recover from the error, or at least print an
1 error message of your choosing before exiting. You can do this in one
1 of two ways:
1
1 * For all output files, by assigning any value to
1 'PROCINFO["NONFATAL"]'.
1
1 * On a per-file basis, by assigning any value to 'PROCINFO[FILENAME,
1 "NONFATAL"]'. Here, FILENAME is the name of the file to which you
1 wish output to be nonfatal.
1
1 Once you have enabled nonfatal output, you must check 'ERRNO' after
1 every relevant 'print' or 'printf' statement to see if something went
1 wrong. It is also a good idea to initialize 'ERRNO' to zero before
1 attempting the output. For example:
1
1 $ gawk '
1 > BEGIN {
1 > PROCINFO["NONFATAL"] = 1
1 > ERRNO = 0
1 > print "hi" > "/no/such/file"
1 > if (ERRNO) {
1 > print("Output failed:", ERRNO) > "/dev/stderr"
1 > exit 1
1 > }
1 > }'
1 error-> Output failed: No such file or directory
1
1 Here, 'gawk' did not produce a fatal error; instead it let the 'awk'
1 program code detect the problem and handle it.
1
1 This mechanism works also for standard output and standard error.
1 For standard output, you may use 'PROCINFO["-", "NONFATAL"]' or
1 'PROCINFO["/dev/stdout", "NONFATAL"]'. For standard error, use
1 'PROCINFO["/dev/stderr", "NONFATAL"]'.
1
1 When attempting to open a TCP/IP socket (⇒TCP/IP Networking),
1 'gawk' tries multiple times. The 'GAWK_SOCK_RETRIES' environment
1 variable (⇒Other Environment Variables) allows you to override
1 'gawk''s builtin default number of attempts. However, once nonfatal I/O
1 is enabled for a given socket, 'gawk' only retries once, relying on
1 'awk'-level code to notice that there was a problem.
1