Bash에서 자동 완성 제안 작업

Bash에서 자동 완성 제안 작업

때로는 bash 프롬프트가 수행할 작업을 제안할 수도 있습니다.

좋다:

The program 'htop' is currently not installed. You can install it by typing:
apt-get install htop

또는:

the branch has no upstream branch yet, do git push --set-upstream origin branchname

!!코드를 복사하거나 다시 입력하지 않고도(예: 마지막 명령 바꾸기) 제안된 작업을 직접 수행할 수 있는 명령, 바로가기 또는 대체가 있습니까 ?

답변1

우선, 당신이 제시한 예는 같은 것이 아닙니다. 우분투에서는Command Not Found 매직은 아래에 설명되어 있습니다.. 더 자세한 내용을 추가하면 실제로 /usr/lib/command-not-found.

예는 다음과 같습니다.

# /usr/lib/command-not-found htop
The program 'htop' is currently not installed. You can install it by typing:
apt-get install htop

/etc/bash.bashrc시작 시 Bash 셸에 포함된 패키지에서 찾을 수 없는 명령 처리기를 정의합니다.

# 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

불다버전 4 이상에서는command_not_found_handle명령을 찾을 수 없는 경우를 처리하기 위한 내장 이름입니다.

이라는 새로운 내장 오류 처리 기능이 있습니다 command_not_found_handle.

#!/bin/bash4

command_not_found_handle ()
{ # Accepts implicit parameters.
  echo "The following command is not valid: \""$1\"""
  echo "With the following argument(s): \""$2\"" \""$3\"""   # $4, $5 ...
} # $1, $2, etc. are not explicitly passed to the function.

bad_command arg1 arg2

# The following command is not valid: "bad_command"
# With the following argument(s): "arg1" "arg2"

따라서 짧은 대답은 '아니요'입니다. 출력을 구문 분석하고 일종의 새로운 기능을 생성하지 않고는 요청한 작업을 수행할 수 있는 방법이 없습니다.

관련 정보