Ubuntu에서 "명령을 찾을 수 없음" 처리기에 연결

Ubuntu에서 "명령을 찾을 수 없음" 처리기에 연결

찾을 수 없는 명령에 대한 처리기에 연결하고 싶습니다.

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를 활성화하고 싶습니다.

해킹은 어디서부터 시작해야 할까요?

답변1

의 경우 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

답변2

일반적으로

그만큼리눅스 저널꽤 좋은 기사가 있습니다:

Bash의 매뉴얼 페이지에서:

... 해시 테이블에서 명령을 찾을 수 없는 경우에만 PATH의 디렉터리 전체 검색이 수행됩니다. 검색에 실패하면 쉘은 command_not_found_handle이라는 정의된 쉘 함수를 검색합니다. 해당 함수가 존재하는 경우 원래 명령과 원래 명령의 인수를 인수로 사용하여 호출되며 함수의 종료 상태는 셸의 종료 상태가 됩니다. 해당 함수가 정의되지 않은 경우 쉘은 오류 메시지를 인쇄하고 종료 상태 127을 반환합니다.

그리고

/etc의 빠른 grep을 통해 해당 문제가 발생한 위치를 발견했습니다. 함수 자체는 /etc/bash_command_not_found에 있으며 해당 함수는 /etc/bash.bashrc를 통해 bash 세션에 포함됩니다(존재하는 경우).

우분투 14.04

경험적 증거에 따르면 Ubuntu 14.04 설치에는 /etc/bash_command_not_found 파일이 존재하지 않지만 올바른 파일은 다음 위치에 있는 Python 스크립트입니다./usr/lib/명령을 찾을 수 없음

관련 정보