Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Old programmers never die, they just hit account block limit.


devel / comp.lang.awk / Re: Options to awk scripts

SubjectAuthor
* Options to awk scriptsSteve Keller
+- Re: Options to awk scriptsJanis Papanagnou
`- Re: Options to awk scriptsKenny McCormack

1
Options to awk scripts

<soi7hi$1jfv$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=963&group=comp.lang.awk#963

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!aioe.org!3bbd7JPLPXs2rcy8LitAsw.user.46.165.242.75.POSTED!not-for-mail
From: keller.steve@gmx.de (Steve Keller)
Newsgroups: comp.lang.awk
Subject: Options to awk scripts
Date: Sun, 5 Dec 2021 12:24:33 +0100
Organization: Aioe.org NNTP Server
Message-ID: <soi7hi$1jfv$1@gioia.aioe.org>
Injection-Info: gioia.aioe.org; logging-data="52735"; posting-host="3bbd7JPLPXs2rcy8LitAsw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
X-Notice: Filtered by postfilter v. 0.9.2
 by: Steve Keller - Sun, 5 Dec 2021 11:24 UTC

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

Re: Options to awk scripts

<soi95k$ovv$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=964&group=comp.lang.awk#964

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou@hotmail.com (Janis Papanagnou)
Newsgroups: comp.lang.awk
Subject: Re: Options to awk scripts
Date: Sun, 5 Dec 2021 12:52:20 +0100
Organization: A noiseless patient Spider
Lines: 61
Message-ID: <soi95k$ovv$1@dont-email.me>
References: <soi7hi$1jfv$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 5 Dec 2021 11:52:20 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="5257777560a78c51243076bda551ad09";
logging-data="25599"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19M6LMdjc9xL9nrExNj0ZEV"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:3u2ghFwBjcTUw+nYAqKsJqN5/Uo=
In-Reply-To: <soi7hi$1jfv$1@gioia.aioe.org>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sun, 5 Dec 2021 11:52 UTC

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
>

Re: Options to awk scripts

<sp8nhq$1qu5s$1@news.xmission.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=965&group=comp.lang.awk#965

  copy link   Newsgroups: comp.lang.awk
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.awk
Subject: Re: Options to awk scripts
Date: Tue, 14 Dec 2021 00:12:42 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sp8nhq$1qu5s$1@news.xmission.com>
References: <soi7hi$1jfv$1@gioia.aioe.org>
Injection-Date: Tue, 14 Dec 2021 00:12:42 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1931452"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Tue, 14 Dec 2021 00:12 UTC

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 -

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor