Почему питон работает здесь?

Ubuntu 20.04. Используя bash. Я сделал опечатки. Я могу упростить:

a </

в основном: неизвестная команда с stdin перенаправлена ​​из каталога.

Я ожидал, что результат будет:

a: command not found

Однако с Ubuntu 20.04 (не Debian) и Bash (не Bitebox, 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
задан 15 March 2021 в 03:14

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 - не -Нугин - который является сценарием Python:

$ file /usr/lib/command-not-found
/usr/lib/command-not-found: Python script, ASCII text executable

с именем функции (в этом случае A ) в качестве аргумента. Однако оболочка уже перенаправлена ​​стандартным входом из / - так что его эффективно вызывает 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
1
ответ дан 18 March 2021 в 23:26

Другие вопросы по тегам:

Похожие вопросы: