gawk: ARGC and ARGV

1 
1 7.5.3 Using 'ARGC' and 'ARGV'
1 -----------------------------
1 
1 ⇒Auto-set presented the following program describing the
1 information contained in 'ARGC' and 'ARGV':
1 
1      $ awk 'BEGIN {
1      >        for (i = 0; i < ARGC; i++)
1      >            print ARGV[i]
1      >      }' inventory-shipped mail-list
1      -| awk
1      -| inventory-shipped
1      -| mail-list
1 
1 In this example, 'ARGV[0]' contains 'awk', 'ARGV[1]' contains
1 'inventory-shipped', and 'ARGV[2]' contains 'mail-list'.  Notice that
1 the 'awk' program is not entered in 'ARGV'.  The other command-line
1 options, with their arguments, are also not entered.  This includes
1 variable assignments done with the '-v' option (⇒Options).
1 Normal variable assignments on the command line _are_ treated as
1 arguments and do show up in the 'ARGV' array.  Given the following
1 program in a file named 'showargs.awk':
1 
1      BEGIN {
1          printf "A=%d, B=%d\n", A, B
1          for (i = 0; i < ARGC; i++)
1              printf "\tARGV[%d] = %s\n", i, ARGV[i]
1      }
1      END   { printf "A=%d, B=%d\n", A, B }
1 
1 Running it produces the following:
1 
1      $ awk -v A=1 -f showargs.awk B=2 /dev/null
1      -| A=1, B=0
1      -|        ARGV[0] = awk
1      -|        ARGV[1] = B=2
1      -|        ARGV[2] = /dev/null
1      -| A=1, B=2
1 
1    A program can alter 'ARGC' and the elements of 'ARGV'.  Each time
1 'awk' reaches the end of an input file, it uses the next element of
1 'ARGV' as the name of the next input file.  By storing a different
1 string there, a program can change which files are read.  Use '"-"' to
1 represent the standard input.  Storing additional elements and
1 incrementing 'ARGC' causes additional files to be read.
1 
1    If the value of 'ARGC' is decreased, that eliminates input files from
1 the end of the list.  By recording the old value of 'ARGC' elsewhere, a
1 program can treat the eliminated arguments as something other than file
1 names.
1 
1    To eliminate a file from the middle of the list, store the null
1 string ('""') into 'ARGV' in place of the file's name.  As a special
1 feature, 'awk' ignores file names that have been replaced with the null
1 string.  Another option is to use the 'delete' statement to remove
1 elements from 'ARGV' (⇒Delete).
1 
1    All of these actions are typically done in the 'BEGIN' rule, before
DONTPRINTYET 1 actual processing of the input begins.  ⇒Split Program and *note1DONTPRINTYET 1 actual processing of the input begins.  ⇒Split Program and ⇒
 Tee Program for examples of each way of removing elements from 'ARGV'.
1 
1    To actually get options into an 'awk' program, end the 'awk' options
1 with '--' and then supply the 'awk' program's options, in the following
1 manner:
1 
1      awk -f myprog.awk -- -v -q file1 file2 ...
1 
1    The following fragment processes 'ARGV' in order to examine, and then
1 remove, the previously mentioned command-line options:
1 
1      BEGIN {
1          for (i = 1; i < ARGC; i++) {
1              if (ARGV[i] == "-v")
1                  verbose = 1
1              else if (ARGV[i] == "-q")
1                  debug = 1
1              else if (ARGV[i] ~ /^-./) {
1                  e = sprintf("%s: unrecognized option -- %c",
1                          ARGV[0], substr(ARGV[i], 2, 1))
1                  print e > "/dev/stderr"
1              } else
1                  break
1              delete ARGV[i]
1          }
1      }
1 
1    Ending the 'awk' options with '--' isn't necessary in 'gawk'.  Unless
1 '--posix' has been specified, 'gawk' silently puts any unrecognized
1 options into 'ARGV' for the 'awk' program to deal with.  As soon as it
1 sees an unknown option, 'gawk' stops looking for other options that it
1 might otherwise recognize.  The previous command line with 'gawk' would
1 be:
1 
1      gawk -f myprog.awk -q -v file1 file2 ...
1 
1 Because '-q' is not a valid 'gawk' option, it and the following '-v' are
1 passed on to the 'awk' program.  (⇒Getopt Function for an 'awk'
1 library function that parses command-line options.)
1 
1    When designing your program, you should choose options that don't
1 conflict with 'gawk''s, because it will process any options that it
1 accepts before passing the rest of the command line on to your program.
1 Using '#!' with the '-E' option may help (⇒Executable Scripts and
1 ⇒Options).
1