Подключитесь к обработчику "команда не найдена" в Ubuntu

1721
wim

Я хочу подключиться к обработчику для команды не найден

wim@SDFA100461C:~$ thing No command 'thing' found, did you mean: Command 'tping' from package 'lam-runtime' (universe) Command 'thin' from package 'thin' (universe) thing: command not found 

Я хочу изменить это поведение своим собственным сценарием.

В частности, я хочу проверить, существует ли команда в выходных данных lsvirtualenv -b, и если да, то я хочу активировать эту virtualenv.

С чего мне начать взлом?

8
Это может помочь: http://askubuntu.com/a/73282/10127 glenn jackman 9 лет назад 1

2 ответа на вопрос

7
Andrew Stubbs

In General

The Linux Journal has a pretty good article:

From bash's man page:

... A full search of the directories in PATH is performed only if the command is not found in the hash table. If the search is unsuccessful, the shell searches for a defined shell function named command_not_found_handle. If that function exists, it is invoked with the original command and the original command's arguments as its arguments, and the function's exit status becomes the exit status of the shell. If that function is not defined, the shell prints an error message and returns an exit status of 127.

and

A quick grep in /etc discovered where it was happening. The function itself is in /etc/bash_command_not_found and that function gets included (if it exists) in your bash session via /etc/bash.bashrc.

Ubuntu 14.04

Empirical evidence suggests that on an Ubuntu 14.04 installation, the file /etc/bash_command_not_found doesn't exist, however, the correct file is a python script, located at /usr/lib/command-not-found

Это поставило меня на правильный путь, но настоящей программой был скрипт на python, расположенный в `/ usr / lib / command-not-found`. В моей установке Ubuntu 14.04 файл `/ etc / bash_command_not_found` не существует. wim 9 лет назад 1
Спасибо, я добавил это в свой ответ для будущих зрителей Andrew Stubbs 9 лет назад 0
1
Randall

Ведь bashего поведение регулируется функцией оболочки command_not_found_handle(см. Раздел « man bashКОМАНДНОЕ ИСПОЛНЕНИЕ»).

Чтобы увидеть, какое поведение определяется этой функцией, вы можете выполнить:

declare -p -f command_not_found_handle 

Вы можете изменить используемую программу, переопределив command_not_found_handleфункцию.

В Ubuntu 14.04 LTS кажется, что поведение по умолчанию определяется непосредственно в /etc/bash.bashrc:

# if the command-not-found package is installed, use it if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found/command-not-found ]; then function command_not_found_handle { # check because c-n-f could've been removed in the meantime if [ -x /usr/lib/command-not-found ]; then /usr/lib/command-not-found -- "$1" return $? elif [ -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" >&2 return 127 fi } fi 

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