Using libraries of awk functions from within an executable awk script

Using libraries of awk functions from within an executable awk script

Post by ptjm » Fri, 02 Apr 2004 14:30:14


In article < XXXX@XXXXX.COM >,


% #!/usr/bin/gawk -f mylib.awk --source
% { print $1; }

I'm not sure if it works on Linux, but what you want is

#!/usr/bin/gawk -f mylib.awk -f
{ print $1; }

this is turned into

/usr/bin/gawk -f mylib.awk -f <scriptname>

by the kernel. Some systems allow several arguments to be given
on the #! line, but some don't. POSIX explicitly doesn't require
it to work at all, as it happens.
--

Patrick TJ McPhee
East York Canada
XXXX@XXXXX.COM
 
 
 

Using libraries of awk functions from within an executable awk script

Post by David Klei » Fri, 02 Apr 2004 16:40:38

Harlan Grove< XXXX@XXXXX.COM > writes:


Because I want the file to be self contained, I.e. I don't want to
type the awk program on the command line, I want it sitting in the file.

--
Use of tools distinguishes Man from Beast. And UNIX users from WINDOZE lusers.

 
 
 

Using libraries of awk functions from within an executable awk script

Post by David Klei » Fri, 02 Apr 2004 16:57:10


XXXX@XXXXX.COM (Patrick TJ McPhee) writes:



Do you know a specific system/shell where this works? I tried this
under bash and it didn't work. Looking closely into the info pages I
found the following:

"The arguments to the interpreter consist of a single optional
argument following the interpreter name on the first line of the script
file, followed by the name of the script file, followed by the rest of
the arguments. Bash will perform this action on operating systems that
do not handle it themselves."

Reading this carefully, I see that it becomes not

/usr/bin/gawk -f mylib.awk -f <scriptname>

like on your system, but instead

/usr/bin/gawk -f <scriptname> mylib.awk -f

And indeed the error message I get is:

gawk: fatal: can't open source file " mylib.awk -f" for reading (No such file or directory)

(note: the " -f mylib.awk")

So I tried instead

#!/usr/bin/gawk -f -f mylib.awk

Hoping to get

/usr/bin/gawk -f <scriptname> -f mylib.awk

But I still got the error message:

gawk: fatal: can't open source file " -f mylib.awk" for reading (No such file or directory)

Changing the '-f' with '--source' didn't help either.

--
Use of tools distinguishes Man from Beast. And UNIX users from WINDOZE lusers.
 
 
 

Using libraries of awk functions from within an executable awk script

Post by Harlan Gro » Fri, 02 Apr 2004 18:07:45

"David Klein" < XXXX@XXXXX.COM > wrote...
...

I was confused.

Can one #! script call another #! script? If so, what about a 2-stage
approach?


$HOME/bin/myawk
---------------
#!/bin/bash
gawk -f $HOME/awk/mylib -f $@


somescript
----------
#!$HOME/bin/myawk
{ print $1 }
 
 
 

Using libraries of awk functions from within an executable awk script

Post by David Klei » Fri, 02 Apr 2004 18:19:15

"Harlan Grove" < XXXX@XXXXX.COM > writes:

<snip>


No, that doesn't work. I am guessing that if myawk was an executable
instead of a script something might be able to be done, but I am not
really a shell expert and I don't know what I would have to put in the
executable.

--
Use of tools distinguishes Man from Beast. And UNIX users from WINDOZE lusers.
 
 
 

Using libraries of awk functions from within an executable awk script

Post by Chris F.A. » Fri, 02 Apr 2004 20:17:21


#!/bin/sh
## USAGE: myscript FILE [...]
script="
BEGIN { x = 666 }
/pattern/ { q[$1] = $2; total += $3 }
END { for ( n in q ) print n, q[n]; print total }
"
echo "$script" | gawk -f $HOME/awk/mylib -f - "$@"


--
Chris F.A. Johnson http://www.yqcomputer.com/
===================================================================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
 
 
 

Using libraries of awk functions from within an executable awk script

Post by David Klei » Fri, 02 Apr 2004 20:33:53

"Chris F.A. Johnson" < XXXX@XXXXX.COM > writes:



<snip>


This doesn't work, since $1 gets expanded inside double-quotes. If you
use single quotes, you are back to my (simpler)

/usr/bin/gawk -f $HOME/awk/mylib --source '
BEGIN { x = 666 }
/pattern/ { q[$1] = $2; total += $3 }
END { for ( n in q ) print n, q[n]; print total }
'

which has the additional advantage of working as a filter on stdin also.

--
Use of tools distinguishes Man from Beast. And UNIX users from WINDOZE lusers.
 
 
 

Using libraries of awk functions from within an executable awk script

Post by Stepan Kas » Fri, 02 Apr 2004 21:55:56

Hi,




No, it becomes:

/usr/bin/gawk "-f mylib.awk -f" <scriptname>

ie. the second argument is the whole rest of the line, containing
the spaces. This is why gawk searches file " mylib.awk -f".

Stepan Kasal
 
 
 

Using libraries of awk functions from within an executable awk script

Post by David Klei » Fri, 02 Apr 2004 23:11:25

Stepan Kasal < XXXX@XXXXX.COM > writes:



I tested this with '#!/bin/echo arg1 arg2' and got
arg1 arg2 <scriptname>

So you are indeed correct. Thanks for pointing it out.

--
Use of tools distinguishes Man from Beast. And UNIX users from WINDOZE lusers.
 
 
 

Using libraries of awk functions from within an executable awk script

Post by ptjm » Sat, 03 Apr 2004 11:42:26

In article < XXXX@XXXXX.COM >,

% XXXX@XXXXX.COM (Patrick TJ McPhee) writes:

% > I'm not sure if it works on Linux, but what you want is
% >
% > #!/usr/bin/gawk -f mylib.awk -f
% > { print $1; }

[...]

% Do you know a specific system/shell where this works? I tried this

It's not a shell thing. At least on Unix systems, this syntax is
processed by the kernel. You can (in a C program) call exec()
with a #! file and it will be executed with no shell involved.

To answer your question, it works on some BSD systems. I've just
tried it here, and it worked on FreeBSD, but not NetBSD.


--

Patrick TJ McPhee
East York Canada
XXXX@XXXXX.COM
 
 
 

Using libraries of awk functions from within an executable awk script

Post by David Klei » Mon, 05 Apr 2004 17:05:53

David Klein < XXXX@XXXXX.COM > writes:


If anyone is interested, The following c program when compiled to an
executable awklib can be used as

#!/home/klein/bin/awklib -f mylib.awk -f
{ print $1; }

Or even

export AWKLIBS="-f /home/klein/bin/mylib.awk -f /home/klein/bin/mylib2.awk"

#!/home/klein/bin/awklib -f
{ print $1; }

Here's the code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[])
{
size_t n;
char *AWKLIBS;
char *p;
char cmd[] = "/usr/bin/gawk ";

if (argc!=3) {
fprintf(stderr, "Usage: #!%s [args] -f\n", argv[0]);
return 1;
}

AWKLIBS = getenv("AWKLIBS");

n = strlen(cmd) + strlen(argv[1]) + strlen(argv[2]) + 3;
if (AWKLIBS) n += strlen(AWKLIBS);
p = malloc(n);

if (!p) {
fprintf(stderr, "Couldn't allocate %d characters. Aborting\n", n);
return 1;
}

sprintf(p,"%s %s %s %s", cmd, (AWKLIBS) ? AWKLIBS : "", argv[1], argv[2]);

system(p);
free(p);

return 0;
}



--
Use of tools distinguishes Man from Beast. And UNIX users from WINDOZE lusers.