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

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
excalibur
帖子: 22
注册时间: 2009-05-30 19:19

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

#1

帖子 excalibur » 2010-09-19 18:23

最近装了一个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
在此很好奇的问一下它是怎么实现的,还往各位知道的告知一二,谢谢
哇吓吓吓吓...
头像
i_NIX
帖子: 450
注册时间: 2008-02-11 15:46

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

#2

帖子 i_NIX » 2010-09-19 19:09

这个,基本来说,就是个类似数据库的东西,查找不到,就给你提示了。
Google Talk群组,请来<雨云>一番!
主题:UbuntuLinux编程科幻、民主、科学等。
添加 [email protected] 为好友(然后say hi,并等待验证)
参见 http://goo.gl/xIpxH

Twitter:http://twitter.com/nixzhu
头像
cnkilior
论坛版主
帖子: 4984
注册时间: 2007-08-05 17:40

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

#3

帖子 cnkilior » 2010-09-19 19:34

也是脚本,是发现并处理127号返回值的函数来做的,这些只不过是它的扩展。
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

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

#4

帖子 aerofox » 2010-09-19 22:57

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
}
头像
excalibur
帖子: 22
注册时间: 2009-05-30 19:19

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

#5

帖子 excalibur » 2010-09-20 16:43

十分感谢各位的解答~
:em01
哇吓吓吓吓...
tusooa
帖子: 6548
注册时间: 2008-10-31 22:12
系统: 践兔
联系:

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

#6

帖子 tusooa » 2010-09-20 17:19

旧版也有的。一直都有的。
只是调用了一个程式而已。
估计,添加很多源之后,发现很慢。

代码: 全选

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

代码: 全选

] ls -ld //
回复