1

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)

7
  • 3
    "test.sh" is a strange name for an awk program. Are you sure that "test.sh" contains that awk code? It's not obvious from your question. I question this because "works for me". Aug 14, 2015 at 15:38
  • If you have a look at the examples you'll see that in one case I've embedded the awk in shell script. File extensions are not significant on Unix/Linux shells.
    – symcbean
    Aug 14, 2015 at 15:53
  • bogus file extensions are significant and confusing for humans though ...
    – jlliagre
    Aug 14, 2015 at 16:05
  • 1
    The #!/bin/env awk -f error is an env problem, as env 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.
    – thrig
    Aug 14, 2015 at 16:24
  • Yes, bit odd when the man page says....env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
    – symcbean
    Aug 14, 2015 at 16:31

1 Answer 1

1

You didn't mention on which OS you are running or which version of awk you're using, but in trying to reproduce your problem on my Linux host with GNU awk (4.1.1), it works fine for me. Given:

#!/bin/awk -f 

BEGIN { exit 1 }

Which when when placed in a file test.sh results in:

$ ./test.sh
$ echo $?
1

I agree with the commenter who said that test.sh is an odd name for an awk script. I would go with a .awk extension to make it clear that it's not actually a shell script.

Update

On RHEL 6.7:

$ rpm -q redhat-release-server
redhat-release-server-6Server-6.7.0.3.el6.x86_64

With awk:

$ awk --version
GNU Awk 3.1.7

Everything seems fine. The script:

$ cat test.awk
#!/bin/awk -f

BEGIN {exit 1}

The result:

$ ./test.awk
$ echo $?
1
4
  • Thanks larsks - I'm running awk 3.1.5 on RHEL 5 - I will dig further to see if an upgrade is a viable solution
    – symcbean
    Aug 14, 2015 at 15:55
  • drat, RHEL 6.6 uses awk 3.1.7 which has the same problem :(
    – symcbean
    Aug 14, 2015 at 15:59
  • Seems towork fine for me on RHEL 6 and on RHEL 5 (although the RHEL 5 environment was not running a RHEL 5 kernel).
    – larsks
    Aug 14, 2015 at 16:24
  • strange. I can't see anything odd in my aliases or environment which might affect this. Got some oddities in my ld.so.conf since Oracle installed here.
    – symcbean
    Aug 14, 2015 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.