21.07.2015 Views

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

GAWK: Effective AWK Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

250 <strong>G<strong>AWK</strong></strong>: <strong>Effective</strong> <strong>AWK</strong> <strong>Programming</strong>}while ((c = getopt(ARGC, ARGV, "a:b:cde")) != -1)......The following program, ‘igawk.sh’, provides this service. It simulates gawk’s searchingof the <strong>AWK</strong>PATH variable and also allows nested includes; i.e., a file that is included with‘@include’ can contain further ‘@include’ statements. igawk makes an effort to only includefiles once, so that nested includes don’t accidentally include a library function twice.igawk should behave just like gawk externally. This means it should accept all of gawk’scommand-line arguments, including the ability to have multiple source files specified via‘-f’, and the ability to mix command-line and library source files.The program is written using the POSIX Shell (sh) command language. 6follows:It works as1. Loop through the arguments, saving anything that doesn’t represent awk source codefor later, when the expanded program is run.2. For any arguments that do represent awk text, put the arguments into a shell variablethat will be expanded. There are two cases:a. Literal text, provided with ‘--source’ or ‘--source=’. This text is just appendeddirectly.b. Source file names, provided with ‘-f’. We use a neat trick and append ‘@includefilename’ to the shell variable’s contents. Since the file-inclusion program worksthe way gawk does, this gets the text of the file included into the program at thecorrect point.3. Run an awk program (naturally) over the shell variable’s contents to expand ‘@include’statements. The expanded program is placed in a second shell variable.4. Run the expanded program with gawk and any other original command-line argumentsthat the user supplied (such as the data file names).This program uses shell variables extensively; for storing command line arguments, thetext of the awk program that will expand the user’s program, for the user’s original program,and for the expanded program. Doing so removes some potential problems that might arisewere we to use temporary files instead, at the cost of making the script somewhat morecomplicated.The initial part of the program turns on shell tracing if the first argument is ‘debug’.The next part loops through all the command-line arguments. There are several casesof interest:-- This ends the arguments to igawk. Anything else should be passed on to theuser’s awk program without being evaluated.-W This indicates that the next option is specific to gawk. To make argumentprocessing easier, the ‘-W’ is appended to the front of the remaining argumentsand the loop continues. (This is an sh programming trick. Don’t worry aboutit if you are not familiar with sh.)6 Fully explaining the sh language is beyond the scope of this book. We provide some minimal explanations,but see a good shell programming book if you wish to understand things in more depth.

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

Saved successfully!

Ooh no, something went wrong!