• Re: Options to awk scripts

    From Janis Papanagnou@21:1/5 to Steve Keller on Sun Dec 5 12:52:20 2021
    On 05.12.2021 12:24, Steve Keller wrote:
    Is there a way to pass options to awk scripts, like

    awk -f script.awk -a

    and handle the -a in my script? It seems when called like above, -a is handled (and not understood) by awk itself. With

    awk -f script.awk -- -a

    I get the -a actually into the ARGV variable. However, I cannot put the -- into the

    #!/usr/bin/awk -f

    and call

    ./script.awk -a

    That seems to be impossible. Probably a shell wrapper

    #!/bin/sh

    exec awk -- '...' $*

    ITYM:

    exec awk '...' "$@"


    will work. Second, how can I handle this in the script? It seems

    BEGIN { if (ARGV[1] == "-a") { opt_a = 1; ARGV[1] = "" } }

    does work. Yet another way would be to handle options in the shell

    #!/bin/sh

    if [ $1 == "-a" ]; then shift; opt=-vopt_a=1; fi
    exec awk $opt '...' $*

    Is there some standard way to do this?

    Personally I wouldn't do that in awk but in shell (using getops) and
    pass the control variables to awk, similar to what you have done, like

    opt_a=0 # and other initializations
    # here comes a while getopts-loop for all options
    case $opt in
    (a) opt_a=1 ;;
    # other options as necessary
    esac

    awk -v opt_a="$opt_a" '...' "$@" # always pass all options!


    Janis


    Steve


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Steve Keller@21:1/5 to All on Sun Dec 5 12:24:33 2021
    Is there a way to pass options to awk scripts, like

    awk -f script.awk -a

    and handle the -a in my script? It seems when called like above, -a is
    handled (and not understood) by awk itself. With

    awk -f script.awk -- -a

    I get the -a actually into the ARGV variable. However, I cannot put the -- into the

    #!/usr/bin/awk -f

    and call

    ./script.awk -a

    That seems to be impossible. Probably a shell wrapper

    #!/bin/sh

    exec awk -- '...' $*

    will work. Second, how can I handle this in the script? It seems

    BEGIN { if (ARGV[1] == "-a") { opt_a = 1; ARGV[1] = "" } }

    does work. Yet another way would be to handle options in the shell

    #!/bin/sh

    if [ $1 == "-a" ]; then shift; opt=-vopt_a=1; fi
    exec awk $opt '...' $*

    Is there some standard way to do this?

    Steve

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to keller.steve@gmx.de on Tue Dec 14 00:12:42 2021
    In article <soi7hi$1jfv$1@gioia.aioe.org>,
    Steve Keller <keller.steve@gmx.de> wrote:
    Is there a way to pass options to awk scripts, like

    awk -f script.awk -a

    and handle the -a in my script? It seems when called like above, -a is >handled (and not understood) by awk itself. With

    Here's a way that works pretty well. I don't know if I'd call it
    "standard" or not...

    --- Cut Here ---
    #!/bin/dash
    exec gawk -f /dev/fd/3 -- "$@" 3<< 'EOF'
    BEGIN { print ARGV[1] }
    # (rest of AWK script goes here)
    --- Cut Here ---

    Run as: ./this -a

    A couple of notes:
    1) /bin/sh on Debian Linux system is actually "dash". So, you could use /bin/sh instead. I prefer to always explicitly name my interpreter, rather than letting it fall to chance.

    2) I really should have put in "EOF" at the end, but I left it out to illustrate that dash doesn't require it. bash does, well, sort of;
    actually it works, but it generates a warning about it.

    --
    "We should always be disposed to believe that which appears to us to be
    white is really black, if the hierarchy of the church so decides."

    - Saint Ignatius Loyola (1491-1556) Founder of the Jesuit Order -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)