{{1}在cd1}中,{1}的问题已经被忽略了import signal
signal.signal(signal.SIGPIPE,signal.SIG_DFL)
您可以尝试从脚本中取消设置SIGPIPE,例如
^{pr2}$
Signals ignored upon entry to the shell cannot be trapped or reset.
最后一句话:这里没有cat的用法;最好将脚本编写为:#!/bin/bash
tr -dc 'a-z1-9' < /dev/urandom | fold -w 4 | head -n 1
但是,既然您使用Bash,那么您最好使用read内置函数,如下所示(这将有利地替换fold和{}):#!/bin/bash
read -n4 a <
printf '%s\n' "$a"
事实证明,使用此版本,您将清楚地知道发生了什么(并且脚本不会挂起):$ python -c "import subprocess; subprocess.call(['./foo'])"
hcwh
tr: write error: Broken pipe
tr: write error
$
$ # script didn't hang
(当然,对于Python3,它工作得很好,没有错误)。告诉Python使用SIGPIPE的默认信号也很好:$ python -c "import signal; import subprocess; signal.signal(signal.SIGPIPE,signal.SIG_DFL); subprocess.call(['./foo'])"
jc1p
$
(也适用于Python3)。在