Jeff Severns Guntzel Jeff Severns Guntzel - 4 years ago 220
Bash Question

Addressing sys.excepthook error in bash script

I've written a bash script that is doing exactly what I want it to do, but kicking out the following error:

close failed in file object destructor: sys.excepthook is missing lost sys.stderr


I'm completely stumped on how to address this. Here is the script:

#!/bin/bash

usage () { echo "${0##*/} inputfile outputfile"; exit 1; }

(($#==2)) || usage

INPUTFILE="$1"
OUTPUTFILE="$2"

# All that is written between between the 'cat' command and
#+ 'EOF' will be sent to the output file.
cat <<EOF >$OUTPUTFILE
$(date "+Generated on %m/%d/%y at %H:%M:%S")

DATA AUDIT: $1

------------
COLUMN NAMES
------------

$(csvcut -n $INPUTFILE)

---------------------------------------
FIRST TEN ROWS OF FIRST FIVE COLUMNS
---------------------------------------

$(csvcut -c 1,2,3,4,5 $INPUTFILE | head -n 10)

------------
COLUMN STATS
------------

$(csvcut $INPUTFILE | csvstat )

---END AUDIT
EOF

echo "Audited!"


I am pretty new to shell scripts and very new to python. I would be grateful for any help.

Answer Source

I was seeing this error when piping output from a Python 2.6.2 script into the head command in bash on Ubuntu 9.04. I added try blocks to close stdout and stderr before exiting the script:

try:
    sys.stdout.close()
except:
    pass
try:
    sys.stderr.close()
except:
    pass

I am no longer seeing the error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download