21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12: A Library of awk Functions 201}argv[i] = ("./" argv[i])BEGIN {if (No_command_assign)disable_assigns(ARGC, ARGV)}You then run your program this way:awk -v No_command_assign=1 -f noassign.awk -f yourprog.awk *The function works by looping through the arguments. It prepends ‘./’ to any argumentthat matches the form of a variable assignment, turning that argument into a file name.The use of No_command_assign allows you to disable command-line assignments at invocationtime, by giving the variable a true value. When not set, it is initially zero (i.e.,false), so the command-line arguments are left alone.12.4 Processing Command-Line OptionsMost utilities on POSIX compatible systems take options, or “switches,” on the commandline that can be used to change the way a program behaves. awk is an example of sucha program (see Section 11.2 [Command-Line Options], page 177). Often, options takearguments; i.e., data that the program needs to correctly obey the command-line option.For example, awk’s ‘-F’ option requires a string to use as the field separator. The firstoccurrence on the command line of either ‘--’ or a string that does not begin with ‘-’ endsthe options.Modern Unix systems provide a C function named getopt for processing command-linearguments. The programmer provides a string describing the one-letter options. If an optionrequires an argument, it is followed in the string with a colon. getopt is also passed thecount and values of the command-line arguments and is called in a loop. getopt processesthe command-line arguments for option letters. Each time around the loop, it returns asingle character representing the next option letter that it finds, or ‘?’ if it finds an invalidoption. When it returns −1, there are no options left on the command line.When using getopt, options that do not take arguments can be grouped together. Furthermore,options that take arguments require that the argument is present. The argumentcan immediately follow the option letter, or it can be a separate command-line argument.Given a hypothetical program that takes three command-line options, ‘-a’, ‘-b’, and‘-c’, where ‘-b’ requires an argument, all of the following are valid ways of invoking theprogram:prog -a -b foo -c data1 data2 data3prog -ac -bfoo -- data1 data2 data3prog -acbfoo data1 data2 data3Notice that when the argument is grouped with its option, the rest of the argument isconsidered to be the option’s argument. In this example, ‘-acbfoo’ indicates that all of the‘-a’, ‘-b’, and ‘-c’ options were supplied, and that ‘foo’ is the argument to the ‘-b’ option.getopt provides four external variables that the programmer can use:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!