(gawk.info) ARGC and ARGV
Info Catalog
(gawk.info) Auto-set
(gawk.info) Built-in Variables
Using `ARGC' and `ARGV'
=======================
In Built-in Variables that Convey Information Auto-set, you
saw this program describing the information contained in `ARGC' and
`ARGV':
$ awk 'BEGIN {
> for (i = 0; i < ARGC; i++)
> print ARGV[i]
> }' inventory-shipped BBS-list
-| awk
-| inventory-shipped
-| BBS-list
In this example, `ARGV[0]' contains `"awk"', `ARGV[1]' contains
`"inventory-shipped"', and `ARGV[2]' contains `"BBS-list"'.
Notice that the `awk' program is not entered in `ARGV'. The other
special command line options, with their arguments, are also not
entered. This includes variable assignments done with the `-v' option
( Command Line Options Options.). Normal variable assignments on
the command line _are_ treated as arguments, and do show up in the
`ARGV' array.
$ cat showargs.awk
-| BEGIN {
-| printf "A=%d, B=%d\n", A, B
-| for (i = 0; i < ARGC; i++)
-| printf "\tARGV[%d] = %s\n", i, ARGV[i]
-| }
-| END { printf "A=%d, B=%d\n", A, B }
$ awk -v A=1 -f showargs.awk B=2 /dev/null
-| A=1, B=0
-| ARGV[0] = awk
-| ARGV[1] = B=2
-| ARGV[2] = /dev/null
-| A=1, B=2
Your program can alter `ARGC' and the elements of `ARGV'. Each time
`awk' reaches the end of an input file, it uses the next element of
`ARGV' as the name of the next input file. By storing a different
string there, your program can change which files are read. You can
use `"-"' to represent the standard input. By storing additional
elements and incrementing `ARGC' you can cause additional files to be
read.
If you decrease the value of `ARGC', that eliminates input files
from the end of the list. By recording the old value of `ARGC'
elsewhere, your program can treat the eliminated arguments as something
other than file names.
To eliminate a file from the middle of the list, store the null
string (`""') into `ARGV' in place of the file's name. As a special
feature, `awk' ignores file names that have been replaced with the null
string. You may also use the `delete' statement to remove elements from
`ARGV' ( The `delete' Statement Delete.).
All of these actions are typically done from the `BEGIN' rule,
DONTPRINTYET before actual processing of the input begins. Splitting a Large
File Into Pieces Split Program, and see *Note Duplicating Output Into
DONTPRINTYET before actual processing of the input begins. Splitting a Large
File Into Pieces Split Program, and see Duplicating Output Into
Multiple Files Tee Program, for an example of each way of removing
elements from `ARGV'.
The following fragment processes `ARGV' in order to examine, and
then remove, command line options.
BEGIN {
for (i = 1; i < ARGC; i++) {
if (ARGV[i] == "-v")
verbose = 1
else if (ARGV[i] == "-d")
debug = 1
else if (ARGV[i] ~ /^-?/) {
e = sprintf("%s: unrecognized option -- %c",
ARGV[0], substr(ARGV[i], 1, ,1))
print e > "/dev/stderr"
} else
break
delete ARGV[i]
}
}
To actually get the options into the `awk' program, you have to end
the `awk' options with `--', and then supply your options, like so:
awk -f myprog -- -v -d file1 file2 ...
This is not necessary in `gawk': Unless `--posix' has been
specified, `gawk' silently puts any unrecognized options into `ARGV'
for the `awk' program to deal with.
As soon as it sees an unknown option, `gawk' stops looking for other
options it might otherwise recognize. The above example with `gawk'
would be:
gawk -f myprog -d -v file1 file2 ...
Since `-d' is not a valid `gawk' option, the following `-v' is passed
on to the `awk' program.
Info Catalog
(gawk.info) Auto-set
(gawk.info) Built-in Variables
automatically generated byinfo2html