分页: 1 / 1

关于新版本(10.04)的bash 提示功能

发表于 : 2010-09-19 18:23
excalibur
最近装了一个Unbuntu 10.04
发现bash很“聪明”的提示你一些东西

比如我们键入

代码: 全选

irb
但是并没没有安装Ruby Interactive的话,bash会提示

代码: 全选

The program 'irb' is currently not installed.  You can install it by typing:
sudo apt-get install irb
在此很好奇的问一下它是怎么实现的,还往各位知道的告知一二,谢谢

Re: 关于新版本(10.04)的bash 提示功能

发表于 : 2010-09-19 19:09
i_NIX
这个,基本来说,就是个类似数据库的东西,查找不到,就给你提示了。

Re: 关于新版本(10.04)的bash 提示功能

发表于 : 2010-09-19 19:34
cnkilior
也是脚本,是发现并处理127号返回值的函数来做的,这些只不过是它的扩展。

Re: 关于新版本(10.04)的bash 提示功能

发表于 : 2010-09-19 22:57
aerofox
man bash 写了:If the name is neither a shell function nor a builtin, and contains no
slashes, bash searches each element of the PATH for a directory
containing an executable file by that name. Bash uses a hash table to
remem‐ ber the full pathnames of executable files (see hash under
SHELL BUILTIN COMMANDS below). 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 argu‐ ments, 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.

代码: 全选

typeset -f command_not_found_handle
command_not_found_handle () 
{ 
    if [ -x /usr/lib/command-not-found ]; then
        /usr/bin/python /usr/lib/command-not-found -- $1;
        return $?;
    else
        if [ -x /usr/share/command-not-found ]; then
            /usr/bin/python /usr/share/command-not-found -- $1;
            return $?;
        else
            return 127;
        fi;
    fi
}

Re: 关于新版本(10.04)的bash 提示功能

发表于 : 2010-09-20 16:43
excalibur
十分感谢各位的解答~
:em01

Re: 关于新版本(10.04)的bash 提示功能

发表于 : 2010-09-20 17:19
tusooa
旧版也有的。一直都有的。
只是调用了一个程式而已。
估计,添加很多源之后,发现很慢。

代码: 全选

tlcr: 0 庚寅年八月十三日 17:14:51 ~
● grep -A2 apt-file ~/.zshrc
apt-file()
{
    sudo apt-file "$@"
    if [ "$1" = update ] ; then
        sudo apt-file search bin/ > "$HOME/.share/programsList"
    fi
}
tlcr: 0 庚寅年八月十三日 17:14:55 ~
● which command_not_found_handler
command_not_found_handler () {
        local l="$(grep /$1$ < $HOME/.share/programsList)"
        if [ "$l" ]
        then
                echo "下列软件包含有命令 $1 :"
                echo "$l" | awk '{print "\033[0;1;32m" $1 "\t\t\033[31m /" $2}'
        else
                echo "未搜索到含有命令 $1 的软件包"
        fi
        return 127
}
tlcr: 0 庚寅年八月十三日 17:15:09 ~
● 
然后apt-file update