分类: 'e-file' 的归档
gentoo也可以提示未安装的命令
用ubuntu的时候,如果你在命令行里输入一个未安装的命令,bash会给出很人性化的提示,让你先安装xxx软件包,比如:
|
1 2 |
程序 'xxx' 尚未安装。 您可以通过输入以下命令安装: sudo apt-get install xxx |
其实gentoo下也是可以做类似提示的.
gentoo早就已经默认是 bash 4.x 了,这个版本的bash,在找不到命令的时候,会试着调用 command_not_found_handle 这个函数,也就是说,只要你在什么地方定义了这个函数,就可以实现人性化的提示.再联想到之前我整的e-file,一切都是这么简单,哈哈~
在 ~/.bashrc 里加上如下几行:
|
1 2 3 4 5 6 7 8 9 |
if echo "`uname -a`" | grep gentoo >/dev/null ; then #由于我的.bashrc是ubuntu和gentoo共用的,所以这里还有些gentoo特有的alias...下面几行才是关键. command_not_found_handle () { echo "-bash: $1: command not found" >&2 e-file $1 >&2 } else #ubuntu的alias.... fi |
当然,这之前肯定要先安装e-file,如果你已经加了gentoo-china的overlay,可以直接 emerge e-file
最终效果贴个图(我是gnome,没装kdevelop):

更新 e-file 到 20081230
更新 e-file 到 20081230 版本
没有其他的该动,只是判断了一下运行脚本的机器是否为gentoo。
如果不是,会有一行警告,其实基本的功能也还是可以用的,当然安装状态之类的肯定就查不到了。
下载地址
e-file 更新至 20081201
很多人反映不喜欢e-file依赖w3m,原因可能是觉得w3m多少有点大,其实我倒是觉得w3m蛮小巧实用的,但是对于一个百来行的脚本来说,依赖个w3m可能确实有点不太合适吧.于是一直想改成依赖curl,因为这个应该默认都有安装.
而且,Daniel老兄(我猜应该是 portagefilelist.de 的人)告诉我,portagefilelist.de 其实是支持 csv 格式输出的,这样对于我的脚本来说,简直是件天大的好事,因为省去了解析烦人的HTML了,哈哈.
于是很快就改好了新的e-file,目前已经更新至gentoo china overlay.
ChangeLog:
20081201:
去掉了w3m的依赖.
代码太长了,贴起来有些难看,呵呵.就不多此一举了,这里的代码应该马上也会更新: http://www.portagefilelist.de/index.php/Tools#e-file_.28serach_from_cmd_line.29
更新了e-file
之前发布的e-file,看起来好像还是挺受欢迎的,没多久,就有人给写了ebuild文件,进了gentoo-china overlay.于是打算再稍微完善一下.
周末去了趟上海,也没空更新这个脚本,拖到今天,终于弄好了,呵呵.
ChangeLog:
20081125:
优化代码结构
增加本地已安装版本的显示
增加Homepage和Description的显示
老套路,贴代码和截图:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
#!/bin/sh #AUTHOR: bones7456 (bones7456<at>gmail<dot>com) ##License: GPL #e-file is like apt-file for gentoo, but data is online VERSION=20081125 function printhelp(){ cat <<EOF This is e-file ($VERSION) Usage: `basename $0` filename EOF } if [[ $# -ne 1 ]];then printhelp exit 1 fi URL="http://www.portagefilelist.de/index.php/Special:PFLQuery2?file=$1&searchfile=lookup&lookup=file#result" w3m -dump -cols 3000 $URL | awk ' BEGIN{ FLAG=0 FOUND=0 "emerge --info | grep PORTDIR" | getline split($0,PORTDIR,/\"/) } { if($1=="dir" && $2=="package"){ FLAG=1 next } if($1=="Retrieved" && $2=="from"){ if(FOUND){ for(pkg in vers){ split(pkg,ii,/\//) NF=0 cmd="ls -tgGd --time-style=+%c /var/db/pkg/" pkg "* 2>/dev/null" cmd | getline if(NF==0){ installed=0 }else{ installed=1 install_time="" for(i=4;i<NF;i++)install_time=install_time " " $i install_time=substr(install_time,2) split($NF,install_arr,pkg "-") } NF=0 cmd="(grep -h HOMEPAGE " PORTDIR[2] "/" pkg "/*.ebuild | tail -n 1)2>/dev/null" cmd | getline if(NF==0){ HOMEPAGE="" }else{ split($0,tempArr,/\"/) HOMEPAGE=tempArr[2] } NF=0 cmd="(grep -h DESCRIPTION " PORTDIR[2] "/" pkg "/*.ebuild | tail -n 1)2>/dev/null" cmd | getline if(NF==0){ DESCRIPTION="" }else{ split($0,tempArr,/\"/) DESCRIPTION=tempArr[2] } setcolor(1,32) if(installed){ printf("[I] ") }else{ printf(" * ") } clearcolor() printf("%s/",ii[1]) setcolor(1,29) printf("%s\n",ii[2]) setcolor(0,32) printf("\tAvailable Versions:\t%s\n",vers[pkg]) if(installed){ printf("\tLast Installed Ver:\t") setcolor(7,34) printf("%s",install_arr[2]) setcolor(0,35) printf("(%s)\n",install_time) setcolor(0,32) } if(HOMEPAGE){ printf("\tHomepage:\t\t") clearcolor() printf("%s\n",HOMEPAGE) setcolor(0,32) } if(DESCRIPTION){ printf("\tDescription:\t\t") clearcolor() printf("%s\n",DESCRIPTION) setcolor(0,32) } printf("\tMatched Files:\t\t") clearcolor() printf("%s\n\n",files[pkg]) } }else{ print "No matches found." } exit } if(FLAG==1 && NF!=0){ T=1 split(vers[$1 "/" $2],vers_arr,/ /) for(i in vers_arr){ if(vers_arr[i]==$NF){ T=0 break } } if(T)vers[$1 "/" $2]=$NF " " vers[$1 "/" $2] T=1 split(files[$1 "/" $2],files_arr,/; /) for(i in files_arr){ if(files_arr[i]==($3 "/" $4)){ T=0 break } } if(T)files[$1 "/" $2]=$3 "/" $4 "; " files[$1 "/" $2] FOUND=1 } } function setcolor(a,b){ printf("%c[%d;%d;%dm",27,2,a,b) } function clearcolor(){ printf("%c[0m",27) } ' |

从图中看到的,其实 portagefilelist.de 的数据也是比较旧的,sys-process/dcron的版本还停留在 2.9 的年代,而本地安装的,早已经是 3.2 了.
脚本下载地址
e-file 根据文件名查询gentoo包的脚本
一直想在gentoo下实现一个类似ubuntu的apt-file的功能,幸好已经有 http://www.portagefilelist.de 这个网站了,就花了2小时写了个小脚本直接到这个站取数据了,感觉效果还可以,先发出来,算是预览版吧,以后会再完善的.
输出格式参考了 eix ,代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#!/bin/sh #AUTHOR: bones7456 (bones7456<at>gmail<dot>com) #VERSION: 20081120 ##License: GPL #e-file is like apt-file for gentoo, but data is online if [[ $# -ne 1 ]];then echo "Usage: `basename $0` filename" exit 1 fi URL="http://www.portagefilelist.de/index.php/Special:PFLQuery2?file=$1&searchfile=lookup&lookup=file#result" w3m -dump -cols 3000 $URL | awk ' BEGIN{ FLAG=0 FOUND=0 } { if($1=="dir" && $2=="package"){ FLAG=1 next } if($1=="Retrieved" && $2=="from"){ if(FOUND){ for(i in ver){ split(i,ii,/\//) printf("%c[%d;%d;%dm* ",27,2,0,32) printf("%c[0m%s/",27,ii[1]) printf("%c[%d;%d;%dm%s\n",27,2,1,29,ii[2]) printf("%c[%d;%d;%dm\t",27,2,0,32) printf("Available versions:\t%s\n",ver[i]) printf("\tMatched File:\t\t") printf("%c[0m",27) printf("%s\n\n",file[i]) } }else{ print "No matches found." } exit } if(FLAG==1 && NF!=0){ ver[$1 "/" $2]=$NF " " ver[$1 "/" $2] file[$1 "/" $2]=$3 "/" $4 FOUND=1 } }' |
截个图:

脚本下载地址