My awk executable is not printing FILENAME...: why?

Hello, there!

I am running an executable awk script with ' source.awk input_file ' and found that when I need to print FILENAME, or ENVIRON["AWKPATH"] or even FNR nothing happens [except for an empty line]...
However, if I run it with ' awk -f source.awk input_file ', then those variables are printed...
What is the reason for that behaviour?
Thanking you in advance.

What shebang are you using on the first line of source.awk? Are you aware that a .awk extension in itself serves no function?

Yes, Scrutinizer, I am aware that the .awk extension is optional; but it should not prevent awk from printing those variables, I think...
As for the shebang, I am using ' #!/usr/local/bin/awk -f '
The source file is on the same directory I use for executable Bash scripts, and the input_file is in another directory.
Only if I call the program with ' awk -f source.awk input_file ' do I get those variables printed.
I am not trying to print them from inside the BEGIN bloc; I've put the print command between blocs and on the END bloc, but it does nothing to make it work...

No error messages? Does it execute at all? Is the x permission set correctly?

No, RudiC: no error messages, and the script does execute well for other instructions.
It is only when attempting to print those variables that I notice something is amiss.
The source.awk has the same permissions as the Bash scripts on the same directory: it does execute, but ignores the instruction where it should print those variables:
{ print FNR, FILENAME, ENVIRON["AWKPATH"] }

FILENAME and FNR are not accessible from the BEGIN block, therefore printing them will return empty string/0 respectively.
For some awk-s FNR is accessible in the END block - for some it's not.
Printing these vars from anywhere BUT BEGIN / END , should work.
What is it that you're trying to achieve? Is your awk==gawk or something else?

1 Like

Does "awk" run "/usr/local/bin/awk" ?
Test with

which awk
type awk

and

/usr/local/bin/awk -f source.awk input_file