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.

202 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>optindoptargopterroptoptThe index in the argument value array (argv) where the first nonoptioncommand-line argument can be found.The string value of the argument to an option.Usually getopt prints an error message when it finds an invalid option. Settingopterr to zero disables this feature. (An application might want to print itsown error message.)The letter representing the command-line option.The following C fragment shows how getopt might process command-line arguments forawk:intmain(int argc, char *argv[]){.../* print our own message */opterr = 0;while ((c = getopt(argc, argv, "v:f:F:W:")) != -1) {switch (c) {case ’f’: /* file */...break;case ’F’: /* field separator */...break;case ’v’: /* variable assignment */...break;case ’W’: /* extension */...break;case ’?’:default:usage();break;}}...}As a side point, gawk actually uses the GNU getopt_long function to process bothnormal and GNU-style long options (see Section 11.2 [Command-Line Options], page 177).The abstraction provided by getopt is very useful and is quite handy in awk programsas well. Following is an awk version of getopt. This function highlights one of the greatestweaknesses in awk, which is that it is very poor at manipulating single characters. Repeated

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

Saved successfully!

Ooh no, something went wrong!