Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Options to awk scripts

28 views
Skip to first unread message

Steve Keller

unread,
Dec 5, 2021, 6:24:37 AM12/5/21
to
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

Janis Papanagnou

unread,
Dec 5, 2021, 6:52:23 AM12/5/21
to
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
>

Kenny McCormack

unread,
Dec 13, 2021, 7:12:44 PM12/13/21
to
In article <soi7hi$1jfv$1...@gioia.aioe.org>,
Steve Keller <keller...@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 -
0 new messages