玩转fortune

fortune是一个非常简单的小程序,几乎存在于所有的unix、bsd、linux操作系统上,它的功能很简单,就是从数据库中随机返回一句话。这个数据库一般是前辈们都设计好了的,也并没有我们常用的关系型数据库那么复杂,一般情况下忽视他们的存在就是了。要使用fortune,在shell中执行就可以了,比如:

$ fortune Q: Do you know what the death rate around here is? A: One per person.

很显然这是一个类似脑筋急转弯的问题。fortune有一些参数可以选用,比如:

$ fortune -c (fortunes) % Never be led astray onto the path of virtue.

-c参数让你知道随机的句子出自哪个文件/或者叫数据库,本例中就是fortunes了。并且,在我使用的ubuntu dapper中,这些数据文件都存储在/usr/share/games/fortune中:

$ ls /usr/share/games/fortunes/ fortunes fortunes.u8 literature.dat riddles riddles.u8 fortunes.dat literature literature.u8 riddles.dat

看来很多人都把fortune当作一个小“游戏”呢。如果想知道fortune都会从哪些文件中随机挑选句子,可以这样:

$ fortune -f 100.00% /usr/share/games/fortunes 52.24% fortunes 31.76% literature 16.00% riddles

-f参数列出fortune所用的所有数据文件,以及他们在随机选取句子过程中的权重。一般这个权重或者比例是按照各个文件中的句子总数来分配的,可以看到ubuntu只带了三个数据文件:fortunes预言,literature名句,riddles谜语。fortune也可以控制每个数据文件的权重大小,比如-e参数让大家平均比例,或者在每个数据文件前面加上百分比:

$ fortune -f 20% fortunes 30% literature riddles 30.00% /usr/share/games/fortunes/literature 20.00% /usr/share/games/fortunes/fortunes 50.00% /usr/share/games/fortunes/riddles

可以看到比例就随之调整了,当然比例只和要等于100%,不过也可以省略riddles前面的百分比,那么它就是100%减去其他文件所占比例之和。还有其他一些参数可以用,比如:

先用-n指定一个句子长度,然后用-l只返回比这个长的,或者用-s只返回比这个短的。 -m regxp返回符合正则表达式的句子 -i在使用-m参数的时候忽略大小写 -o只返回具有攻击性的句子(不建议在网上对他人使用) -a在所有句子中随机选择,包括具有攻击性或者不具有的。 -w 程序结束前等待一段时间,如果每次关机之前来一下,可以让你有时间来阅读

除了系统给定的那些数据文件之外,我们当然想自己创建啦,这个就要用到strfile命令了,首先来创建一个数据文件test:

test1 % test2 % test3

句子可以是多行,句子之间用独占一行的字符%来分割,然后用strfile为test创建索引文件,就可以在fortune中使用了:

$ strfile test “test.dat” created There were 3 strings Longest string: 7 bytes Shortest string: 6 bytes $ fortune test test3

注意如果不使用strfile创建索引,数据是不会被索引到的,所以要在新建和每次更新数据文件之后重新创建索引。

用法就介绍到这里,那么fortune有什么用呢?这就要看用户们怎么发挥了,比如我想在mutt的签名中随机附上一个脑筋急转弯,或者为网站生成“每日技巧”等等都可以,nix下的工具就是这样,只作一件事,并且作到最好,然后由用户来自由组合。

谁有兴趣把唐诗宋词弄进来玩玩?

update @ 2006-12-09 循着gorf留言的提示,顺利在debian的ftp中下载了fortune-zh 1.6版,把其中的song100和tang300两个文件抓过来就是唐诗宋词了,strfile转换后顺利应用于我的ubuntu中。

$ strfile song100 “song100.dat” created There were 95 strings Longest string: 955 bytes Shortest string: 175 bytes $ strfile tang300 “tang300.dat” created There were 313 strings Longest string: 2991 bytes Shortest string: 119 bytes

准确的说,宋词是95首,唐诗是313首 :-)。

然后,在aptitude中还发现ubuntu源中也有更多的fortune数据文件,比如debian的每日技巧:

$apt-get install fortunes-debian-hints $ fortune debian-hints Debian Hint #15: The documentation for a package can normally be found under /usr/share/doc/<package>. In particular, the README.Debian file often has useful information about Debian-specific quirks or tips. </package>

甚至从垃圾邮件中收集来的信息,注意这个会安装到off目录下,属于攻击性或者不太适合的内容,慎用:

$apt-get install fortunes-spam $ fortune spam Today’s spam: blessed in the name of the Lord.

还有牛人mario打包的超级数据文件,分为好几个文件,比如mario.arteascii

$apt-get install fortunes-mario
$ fortune mario.arteascii
$ fortune mario.arteascii
-=Watch face=-

.---. /,-"0"-.\ // | \\ ||9 o 3|D \ . // \-.6.-'/ `=---=' 这就是一个超级ascii艺术图案宝库啊。

3 thoughts on “玩转fortune”

Leave a Reply to siko Cancel reply

Your email address will not be published. Required fields are marked *