![为什么python在这里运行?](https://linux22.com/image/1022195/%E4%B8%BA%E4%BB%80%E4%B9%88python%E5%9C%A8%E8%BF%99%E9%87%8C%E8%BF%90%E8%A1%8C%EF%BC%9F.png)
Ubuntu 20.04。使用 bash。我打错了。我可以简化为:
a </
基本上:从目录重定向带有 stdin 的未知命令。
我期望的结果是:
a: command not found
但是使用 Ubuntu 20.04(不是 debian)和 bash(不是 busybox、dash、ksh、tcsh)时,我得到:
Fatal Python error: init_sys_streams: <stdin> is a directory, cannot continue
Python runtime state: core initialized
Current thread 0x00007f83aacaa740 (most recent call first):
<no Python frame>
发生了什么事?为什么 Python 会参与其中?
答案1
Bash 通过一个函数实现此功能command_not_found_handle
:
$ declare -f -p command_not_found_handle
command_not_found_handle ()
{
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1";
return $?;
else
if [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1";
return $?;
else
printf "%s: command not found\n" "$1" 1>&2;
return 127;
fi;
fi
}
如您所见,该函数尝试调用/usr/lib/command-not-found
- 这是一个python脚本:
$ file /usr/lib/command-not-found
/usr/lib/command-not-found: Python script, ASCII text executable
使用函数名称(在本例中为a
)作为参数。但是 shell 已经将标准输入重定向至/
- 因此它实际上是python3
使用输入重定向进行调用:
$ python3 </
Fatal Python error: init_sys_streams: <stdin> is a directory, cannot continue
Python runtime state: core initialized
Current thread 0x00007f16b0ab0740 (most recent call first):
<no Python frame>
如果使用以下命令运行命令,则可以更清楚地看到该序列set -x
:
$ set -x
$
$ a </
+ a
+ '[' -x /usr/lib/command-not-found ']'
+ /usr/lib/command-not-found -- a
Fatal Python error: init_sys_streams: <stdin> is a directory, cannot continue
Python runtime state: core initialized
Current thread 0x00007feb94250740 (most recent call first):
<no Python frame>
+ return 1