I\'d like to execute an gawk script with --re-interval
using a shebang. The \"naive\" approach of
#!/usr/bin/gawk --re-interval -f
... awk scri
In the gawk manual (http://www.gnu.org/manual/gawk/gawk.html), the end of section 1.14 note that you should only use a single argument when running gawk from a shebang line. It says that the OS will treat everything after the path to gawk as a single argument. Perhaps there is another way to specify the --re-interval
option? Perhaps your script can reference your shell in the shebang line, run gawk
as a command, and include the text of your script as a "here document".
For a portable solution, use awk
rather than gawk
, invoke the standard BOURNE shell (/bin/sh
) with your shebang, and invoke awk
directly, passing the program on the command line as a here document rather than via stdin:
#!/bin/sh
gawk --re-interval <<<EOF
PROGRAM HERE
EOF
Note: no -f
argument to awk
. That leaves stdin
available for awk
to read input from. Assuming you have gawk
installed and on your PATH
, that achieves everything I think you were trying to do with your original example (assuming you wanted the file content to be the awk script and not the input, which I think your shebang approach would have treated it as).
Why not use bash
and gawk
itself, to skip past shebang, read the script, and pass it as a file to a second instance of gawk [--with-whatever-number-of-params-you-need]
?
#!/bin/bash
gawk --re-interval -f <(gawk 'NR>3' $0 )
exit
{
print "Program body goes here"
print $1
}
(-the same could naturally also be accomplished with e.g. sed
or tail
, but I think there's some kind of beauty depending only on bash
and gawk
itself;)
Just for fun: there is the following quite weird solution that reroutes stdin and the program through file descriptors 3 and 4. You could also create a temporary file for the script.
#!/bin/bash
exec 3>&0
exec <<-EOF 4>&0
BEGIN {print "HALLO"}
{print \$1}
EOF
gawk --re-interval -f <(cat 0>&4) 0>&3
One thing is annoying about this: the shell does variable expansion on the script, so you have to quote every $ (as done in the second line of the script) and probably more than that.