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.

Chapter 13: Practical awk Programs 251-v, -FThese are saved and passed on to gawk.-f, --file, --file=, -Wfile=The file name is appended to the shell variable program with an ‘@include’statement. The expr utility is used to remove the leading option part of theargument (e.g., ‘--file=’). (Typical sh usage would be to use the echo and sedutilities to do this work. Unfortunately, some versions of echo evaluate escapesequences in their arguments, possibly mangling the program text. Using expravoids this problem.)--source, --source=, -Wsource=The source text is appended to program.--version, -Wversionigawk prints its version number, runs ‘gawk --version’ to get the gawk versioninformation, and then exits.If none of the ‘-f’, ‘--file’, ‘-Wfile’, ‘--source’, or ‘-Wsource’ arguments are supplied,then the first nonoption argument should be the awk program. If there are no commandlinearguments left, igawk prints an error message and exits. Otherwise, the first argumentis appended to program. In any case, after the arguments have been processed, programcontains the complete text of the original awk program.The program is as follows:#! /bin/sh# igawk --- like gawk but do @include processingif [ "$1" = debug ]thenset -xshiftfi# A literal newline, so that program text is formatted correctlyn=’’# Initialize variables to emptyprogram=opts=while [ $# -ne 0 ] # loop over argumentsdocase $1 in--) shift; break;;-W)shift# The ${x?’message here’} construct prints a# diagnostic if $x is the null stringset -- -W"${@?’missing operand’}"continue;;

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

Saved successfully!

Ooh no, something went wrong!