I'm writing some awk scripts for data validation. These are intended to be selected and invoked by another proces which needs to see the exit code from the awk script to determine if the content was valid or not. But I'm having trouble reading the exit the exit code:
I wrote this script to attempt to reproduce the program and ran it...
#!/bin/awk -f
BEGIN { exit 1 }
$ ./test.sh ; echo $?
0
I had expected the output to be 1. Immediately suspecting that awk was wrong and I was right, I tried this:
$ awk 'BEGIN { exit 1 }' ; echo $?
1
So awk's exit does sem to do what I want, but not when run from a shebang script.
I then tried:
#!/bin/env awk -f
BEGIN { exit 1 }
$ ./test.sh ; echo $?
0
/bin/env: awk -f: No such file or directory
While I could do this....
#!/bin/sh
awk 'BEGIN { exit 1 }'
$ ./test.sh ; echo $?
1
I then loose all the syntax highlighting/manipulation of the script (upwards of 200 lines) in vi.
Again, another option would be:
#!/bin/sh
awk -f $1
exit $?
But that then requires managing the path to the awk script as well has the wrapping executable (${BASH_SOURCE[0]} only works for bash).
Ideally I also want to also set the '-W posix' option for awk, but that adds further complications!
Update
Running the first script above on OpenSuse 13 / awk 4.1.0 I get the expected result (sadly that doesn't actually solve the problem)
#!/bin/env awk -f
error is anenv
problem, asenv
is looking to execute the literal file "awk-space-hyphen-f" from somewhere in your PATH. Same error if you try a#!/usr/bin/env ls -al
hashbang line, for example.env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]