Using awk getopts 
Author Message
 Using awk getopts

A little confused here how one utilizes the nifty functions that come
with the gawk distro.   In this case, I'd like to use the `awk
getopts' function.

Arnold R. Has thoughtfully provided a breakdown of the code itself
but I have lots of trouble following along.

What is not clear is how this library function is accessed.

For example:  If one wanted to write a simple awk calculator and allow
command line switchs like -d for divide -m for multiply etc.
I can visualize doing this with shell scripting easily:

Shell (cat shell.awk.calc)
 while getopts blah blah
 d) shift
        awk 'BEGIN{print ('"$1"' / '"$2"')}'
        ;;
 m) shift
        awk 'BEGIN{print ('"$1"' * '"$2"')}'
        ;;
and so forth.

Called like `shell.awk.calc -m 10.4  3.9'

But how to proceed with an executable awk script: Hoping someone will
provide a simple 1 or 2 opt example.

Something that isn't entirely clear is whether the function can be
used from a library somewhere else on the file system by calling it in
the script or must be included in the script itself.

I suppose I could just use a series like:
(untested)
BEGIN {
 if (ARGV[1] ~ "-m" ) {
        print ( ARGV[2] * ARGV[3])
        }

Quote:
}

But then I wouldn't be using the nifty functions...


Sat, 06 Dec 2003 00:20:28 GMT  
 Using awk getopts

Quote:
> A little confused here how one utilizes the nifty functions that come
> with the gawk distro.   In this case, I'd like to use the `awk
> getopts' function.

> Arnold R. Has thoughtfully provided a breakdown of the code itself
> but I have lots of trouble following along.

> What is not clear is how this library function is accessed.

Well I usually use it like this:

$ awk -f getopts.awk -f myprog.awk -- -arg1 -arg2

you can use as many "-f"s as your system will allow.

Quote:
> For example:  If one wanted to write a simple awk calculator and allow
> command line switchs like -d for divide -m for multiply etc.
> I can visualize doing this with shell scripting easily:

> Shell (cat shell.awk.calc)
>  while getopts blah blah
>  d) shift
>         awk 'BEGIN{print ('"$1"' / '"$2"')}'
>         ;;
>  m) shift
>         awk 'BEGIN{print ('"$1"' * '"$2"')}'
>         ;;
> and so forth.

> Called like `shell.awk.calc -m 10.4  3.9'

> But how to proceed with an executable awk script: Hoping someone will
> provide a simple 1 or 2 opt example.

Something along these lines maybe?

$ awk -f getopts.awk -f calc.awk -- -m 10.4 3.9

the "--" is so that the following options are interpreted by getopt and
not awk itself.

Quote:
> Something that isn't entirely clear is whether the function can be
> used from a library somewhere else on the file system by calling it in
> the script or must be included in the script itself.

Checkout the section "Running awk" and the description of the AWKPATH
variable in the the gawk manual.

<snip>

HTH

--
Peter S Tillier
peter dot tillier at btinternet dot com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Mon, 22 Dec 2003 12:45:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. getopts and AWK

2. Outputting info from awk and using the info to name the awk output file

3. getopts for Clean

4. getopts for haskell

5. doc for getopts

6. What happened to getopts.rb?

7. getopts PLI for verilog?

8. getopts confusion

9. getopts confusion

10. Newbie Getopts Question

11. NewBie - GetOpts

12. getopts type procedure

 

 
Powered by phpBB® Forum Software