在 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

首先,你举的例子不是同一件事。在 Ubuntu 下,Command Not Found Magic 解释如下。添加更多细节,它实际上是在/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 shell 包含的中,我们定义了 command-not-found 处理程序:

# 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"

所以,简短的答案是否定的,如果不解析输出并创建某种新功能,就无法执行您所要求的操作。

相关内容