解决 bash 脚本中的 sys.excepthook 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7955138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Addressing sys.excepthook error in bash script
提问by Jeff Severns Guntzel
I've written a bash script that is doing exactly what I want it to do, but kicking out the following error:
我编写了一个 bash 脚本,它完全按照我的意愿行事,但排除了以下错误:
close failed in file object destructor: sys.excepthook is missing lost sys.stderr
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=""
OUTPUTFILE=""
# 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:
------------
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.
我对 shell 脚本很陌生,对 python 也很陌生。我将不胜感激任何帮助。
回答by George
I was seeing this error when piping output from a Python 2.6.2 script into the headcommand in bashon Ubuntu 9.04. I added tryblocks to close stdoutand stderrbefore exiting the script:
将 Python 2.6.2 脚本的输出通过管道传输到Ubuntu 9.04 中的head命令时,我看到了此错误bash。我加了try块接近stdout和stderr离开脚本之前:
try:
sys.stdout.close()
except:
pass
try:
sys.stderr.close()
except:
pass
I am no longer seeing the error.
我不再看到错误。
回答by berniey
There are two steps you need to perform:
您需要执行两个步骤:
Step 1:
第1步:
In your csvcutscript, find all locations where sys.stdout.write()are called, make sure sys.stdout.flush()is called after each write().
在您的csvcut脚本中,找到所有sys.stdout.write()被调用的位置,确保sys.stdout.flush()在每个write().
Step 2:
第2步:
With step 1 completed you should now be able to capture IOErrorwithin the Python script. Below is one example on how to handle broken pipe:
完成第 1 步后,您现在应该能够IOError在 Python 脚本中进行捕获。下面是一个关于如何处理破损管道的例子:
try:
function_with_sys_stdout_write_call()
except IOError as e:
# one example is broken pipe
if e.strerror.lower() == 'broken pipe':
exit(0)
raise # other real IOError
Hope it helps!
希望能帮助到你!
回答by ZenGyro
I assume that the csvcutPython script is otherwise functional but is throwing up an error when it tries to close files and exit.
我假设csvcutPython 脚本在其他方面可以正常运行,但是在尝试关闭文件并退出时会抛出错误。
If, as you say, the script is otherwise working and assuming that the error 'csvcut' is throwing up outputs to stderr then redirecting it to /dev/null would be a temp fix.
如果,如您所说,脚本以其他方式工作并假设错误 'csvcut' 将输出抛出到 stderr,则将其重定向到 /dev/null 将是一个临时修复。
cat <<EOF >$OUTPUTFILE 2>/dev/null
Naturally any other error messages in your heredoc will also redirect there.
当然,您的 heredoc 中的任何其他错误消息也会重定向到那里。

