我如何在Ubuntu中教bash一些诅咒的话?


21

当bash遇到未知命令(单词?)时,它将执行以下操作:

The program 'hello' can be found in the following packages:
 * hello
 * hello-debhelper
Try: sudo apt-get install <selected package>

我想知道这是如何完成的,因此我可以对其进行编辑或添加一些内容,以便从本国字典中交叉检查未知词,该词具有短语:答复对,然后可以将其发送至输出。

我感到内looking ..但是我尝试挖掘的一些bash指南对此没有任何帮助。也许我在看错地方..任何指针?

是的,我正在这样做,所以每当程序失败时我键入wtf,我都希望向我扔东西……


1
当我们使用它时,如何将其完全禁用?
2011年


Answers:


21

查找您/etc/bash.bashrccommand_not_found_handle函数定义。

如果要删除该行为,请将其放入.bashrc中

[[ $(type -t command_not_found_handle) = "function" ]] && 
  unset -f command_not_found_handle

如果要自定义,可以执行

# see http://stackoverflow.com/questions/1203583/how-do-i-rename-a-bash-function
alias_function() {
  eval "${1}() $(declare -f ${2} | sed 1d)"
}

alias_function orig_command_not_found_handle command_not_found_handle 

command_not_found_handle() {
  command=$1
  shift
  args=( "$@" )

  do your stuff before
  orig_command_not_found_handle "$command" "${args[@]}"
  do your stuff after
}

1
我喜欢这种方法。
ændrük

1
哇!我喜欢alias_function的想法:-)
13年

要查看/检查当前定义,请执行以下操作:declare -p -f command_not_found_handle
Randall

4

可能很有用...

找不到命令的程序包为您提供了神奇的响应。我不确定是否可以对其进行自定义,但是可能值得一看。

我认为要执行的操作的另一种选择是在.bashrc文件中添加一个别名,该别名将在您每次键入'wtf'或类似的东西时显示一条消息:

alias wtf='echo "chill out man"'

将此添加到〜/ .bashrc文件,然后执行以下操作: source $HOME/.bashrc

然后,只要您wtf在终端输入内容,它就只会打印一条消息。您还可以使该别名称为脚本,该脚本将打印更详细的消息或类似内容。可能性是无止境!


3

此行为在系统范围的Bash配置文件中定义/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 ]; 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/bin/python /usr/lib/command-not-found -- "$1"
      return $?
    elif [ -x /usr/share/command-not-found ]; then
      /usr/bin/python /usr/share/command-not-found -- "$1"
      return $?
    else
      return 127
    fi
  }
fi

要对其进行自定义,只需重写自己的函数即可~/.bashrc

function command_not_found_handle {
  echo "Sorry, smotchkiss, try again."
}

0

@ user606723,如果要完全摆脱此行为:

sudo apt-get remove command-not-found command-not-found-data 

如果这不起作用,请尝试以下操作:

sudo apt-get purge command-not-found command-not-found-data 

如果您想恢复行为:

sudo apt-get install command-not-found
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.