manpagez: man pages & more
info gawk
Home | html | info | man

gawk: Exit Statement

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