2
I seem to have stumbled on something which is probably a bug in awk, but it could also be a bug in my understanding of bash/awk.
I was trying to debug issues where the output of a python program was being piped to awk and I would get the following exception irrespective of what the awk command was doing.
close failed in file object destructor:
Error in sys.excepthook:
Original exception was:
As it turns out awk was getting passed an empty first argument, followed by -f awkfilename.awk
. So the error can be reproduced by the following command line:
python -c 'print "hello"' | awk ''
But If I run awk without any arguments (which is what I would consider above to be the equivalent of), I get the awk help followed by the same exception
python -c 'print "hh"' | awk
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options:
-f progfile --file=progfile
-F fs --field-separator=fs
-v var=val --assign=var=val
-m[fr] val
-W compat --compat
-W copyleft --copyleft
-W copyright --copyright
-W dump-variables[=file] --dump-variables[=file]
-W exec=file --exec=file
-W gen-po --gen-po
-W help --help
-W lint[=fatal] --lint[=fatal]
-W lint-old --lint-old
-W non-decimal-data --non-decimal-data
-W profile[=file] --profile[=file]
-W posix --posix
-W re-interval --re-interval
-W source=program-text --source=program-text
-W traditional --traditional
-W usage --usage
-W use-lc-numeric --use-lc-numeric
-W version --version
To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.
gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.
Examples:
gawk '{ sum += $1 }; END { print sum }' file
gawk -F: '{ print $1 }' /etc/passwd
close failed in file object destructor:
Error in sys.excepthook:
Original exception was:
Note: the message after "Original Exception was:" is actually empty, its not something I have skipped.
Details about my system
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
$ awk --version
GNU Awk 3.1.6
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04.3 LTS
$ uname -a
Linux <hostname> 2.6.32-37-generic #81-Ubuntu SMP Fri Dec 2 20:32:42 UTC 2011 x86_64 GNU/Linux
I would be happy if someone could offer some insight. Ofcourse, the immediate solution is to sanitize the argument that gets passed as empty to awk which I have done, but this made me curious about the cause.
Edits
Based on comments below I udnerstand that awk
and awk ''
are different in that the second invocation means awk sees the number of arguments to be 1 (with the argument being empty string) instead 0.
What I still dont understand is what does the empty string as the awk expression do.
For e.g. the following works fine
$ echo "" > /tmp/empty.awk
$ python -c 'print "hello"' | awk -f /tmp/empty.awk
$ echo $?
$ 0
So if I understand you correctly, you are implying if I run awk with an empty script, awk doesnt read STDIN which is why python writing to STDOUT encounters the error? If this were valid, then I should encounter the same error if I ran awk with a file that was empty, correct? Like
python -c 'print "hello"' | awk -f /tmp/empty.awk
. The empty awk script passed with -f doesnt reproduce the error. – Puneet – 2012-02-07T01:46:08.440@Puneet it's a matter of timing (known as a race condition), depending on exactly how long the two commands take. Compare the results of
– Gordon Davisson – 2012-02-07T03:13:54.603{ sleep 0; python -c 'print "hello"'; } | { sleep 1; awk -f /tmp/empty.awk; }
vs. `{ sleep 1; python -c 'print "hello"'; } | { sleep 0; awk -f /tmp/empty.awk; }Also, if your awk script contains some rules other than
BEGIN {...}
, then it will consume all stdin before exiting; so in that case too you should encounter no error. You could achieve this by using'{}'
as your awk script; you don't achieve it by using the empty string''
. – dubiousjim – 2012-04-19T11:48:36.940