gawk: Exit Statement

1 
1 7.4.10 The 'exit' Statement
1 ---------------------------
1 
1 The 'exit' statement causes 'awk' to immediately stop executing the
1 current rule and to stop processing input; any remaining input is
1 ignored.  The 'exit' statement is written as follows:
1 
1      'exit' [RETURN CODE]
1 
1    When an 'exit' statement is executed from a 'BEGIN' rule, the program
1 stops processing everything immediately.  No input records are read.
1 However, if an 'END' rule is present, as part of executing the 'exit'
1 statement, the 'END' rule is executed (⇒BEGIN/END).  If 'exit' is
1 used in the body of an 'END' rule, it causes the program to stop
1 immediately.
1 
1    An 'exit' statement that is not part of a 'BEGIN' or 'END' rule stops
1 the execution of any further automatic rules for the current record,
1 skips reading any remaining input records, and executes the 'END' rule
1 if there is one.  'gawk' also skips any 'ENDFILE' rules; they do not
1 execute.
1 
1    In such a case, if you don't want the 'END' rule to do its job, set a
1 variable to a nonzero value before the 'exit' statement and check that
1 variable in the 'END' rule.  ⇒Assert Function for an example that
1 does this.
1 
1    If an argument is supplied to 'exit', its value is used as the exit
1 status code for the 'awk' process.  If no argument is supplied, 'exit'
1 causes 'awk' to return a "success" status.  In the case where an
1 argument is supplied to a first 'exit' statement, and then 'exit' is
1 called a second time from an 'END' rule with no argument, 'awk' uses the
1 previously supplied exit value.  (d.c.)  ⇒Exit Status for more
1 information.
1 
1    For example, suppose an error condition occurs that is difficult or
1 impossible to handle.  Conventionally, programs report this by exiting
1 with a nonzero status.  An 'awk' program can do this using an 'exit'
1 statement with a nonzero argument, as shown in the following example:
1 
1      BEGIN {
1          if (("date" | getline date_now) <= 0) {
1              print "Can't get system date" > "/dev/stderr"
1              exit 1
1          }
1          print "current date is", date_now
1          close("date")
1      }
1 
1      NOTE: For full portability, exit values should be between zero and
1      126, inclusive.  Negative values, and values of 127 or greater, may
1      not produce consistent results across different operating systems.
1