美国中餐馆的最后一道菜,往往是小甜饼,叫做"幸运饼"(fortune cookie)。
里面有一张纸条,写着人生格言。
这种形式的格言,显然很受欢迎。早在1979年,就有人写了一个叫做 fortune 的小程序。在命令行下输入fortune,就会跳出一句。
通常,Linux发行版自带这个程序。如果没有,需要先安装这个程序。
# Debian/Ubuntu $ sudo apt-get install fortune # Mac $ brew install fortune
我很喜欢这个程序,但是它默认的格言库都是英语的,我觉得这点不好,就自己动手做了一个中文格言库。上图就来自这个库,安装命令如下。
$ git clone [email protected]:ruanyf/fortunes.git $ sudo mv fortunes/data/* /usr/share/games/fortunes/
如果是Mac系统,需要重新生成索引文件,即在上面两个命令之间,增加几个命令。
$ strfile fortunes/data/fortunes $ strfile fortunes/data/chinese $ strfile fortunes/data/tang300 $ strfile fortunes/data/song100
目前,这个库带有四个格言包。
- fortunes:英语格言,5472条
- chinese:中文格言,25919条
- tang300:唐诗三百首,313条
- song100::宋词一百首,95条
fortune 命令显示的格言是随机的,但是分布比例与格言包的容量一致。如果希望四个包平均显示,即每个包都有25%的显示机会,需要使用下面的命令格式。
$ fortune -e fortunes chinese tang300 song100 # 或者 $ fortune 25% fortunes 25% chinese 25% tang300 25% song100
上面代码的第二种形式,让你可以根据自己的偏好,调节每个包的比重。
在 ~/.bashrc 或 ~/.zshrc 文件(根据你使用的 shell 而定)的结尾,加上下面几行,那么每次启动 shell 窗口,就会自动跳出一句格言。
echo echo "=============== Quote Of The Day ===============" echo fortune echo echo "================================================" echo
格言包就是一个文本文件,可以放入任何内容。假如你想用它背单词,也没有问题。下面就是如何制作这一类的 fortune 数据文件。
(1)所有条目都写入一个文本文件,文件名任意。
(2)条目之间用单独一行的百分号(%)分隔,就像下面这样。
路漫漫其修远兮,吾将上下而求索。
----屈原《离骚》
%
富贵不能淫,贫贱不能移,威武不能屈。
----《孟子》
%
长风破浪会有时,直挂云帆济沧海。
----《行路难·其一》
%
志不强者智不达,言不信者行不果。
----墨子
%
锲而舍之,朽木不折;锲而不舍,金石可镂。
----《荀子·劝学》
%
(3)生成索引文件。
$ strfile <fortuneFile> <fortuneFile.dat>
上面命令中,尖括号的 fortune 文件名,替换成你的文件名。
(4)fortune 数据文件和它的索引文件,都拷贝到目录 /usr/share/games/fortunes/ 。
(完)
hmy 说:
debian下很早就有这个中午包了,全是唐诗宋词,fortune-zh
ps: 我用这个来做git 提交的commit
2015年4月14日 10:18 | # | 引用
鸸鹋先生 说:
英文的可以配合toilet耍耍。
2015年4月14日 10:19 | # | 引用
fengchang 说:
谢谢阮老师,很棒,已经换上
2015年4月14日 11:29 | # | 引用
frank 说:
fortune chinese 为何输出整个文件,而不是一段话呢?
❯ fortune -v
fortune-mod version 9708
2015年4月14日 12:56 | # | 引用
i 说:
fortune-mod上游已死- -
2015年4月14日 13:01 | # | 引用
i 说:
/etc/profile.d/fortune.sh
case $- in
*i* ) echo;fortune ascii-art computers debian fortunes linux linuxcookie science;echo;;
esac
2015年4月14日 13:06 | # | 引用
吾爱 说:
不知道为什么
$ fortune -e fortunes chinese tang300 song100
# 或者
$ fortune 25% fortunes 25% chinese 25% tang300 25% song100
无法生效,也不报错,它还是按照默认比例。没办法,只能把所有 .dat 文件删掉,只生成指定几个库的索引,比例无法指定。
2015年4月14日 14:12 | # | 引用
yannisxu 说:
+1 中文输出的是整个文件 OS X
2015年4月14日 17:10 | # | 引用
阮一峰 说:
@yannisxu,@frank,
我没有Mac,能够运行一下strfile,告诉我结果吗?
可以在fortune/data目录中,运行下面命令。
$ strfile song100
2015年4月14日 17:39 | # | 引用
阮一峰 说:
@吾爱:
fortune命令后直接跟格言包文件名,就可以了。
$ fortune tang300
2015年4月14日 17:51 | # | 引用
frank 说:
/u/l/C/f/9/s/g/fortunes ❯❯❯ strfile song100
"song100.dat" created
There were 95 strings
Longest string: 955 bytes
Shortest string: 175 bytes
/u/l/C/f/9/s/g/fortunes ❯❯❯ strfile chinese
"chinese.dat" created
There were 25919 strings
Longest string: 2620 bytes
Shortest string: 1 byte
2015年4月14日 20:13 | # | 引用
frank 说:
好像 strfile 一下就可以了耶!
2015年4月14日 20:14 | # | 引用
frank 说:
Mac 下用 brew install fortune 安装,安装后 ~/.zlogin 自动会添加 fortune -a 命令,改成你的命令即可。
然后把你的每个数据文件 strfile 一下,就 OK 了。
2015年4月14日 20:22 | # | 引用
fadeoc 说:
没有windows差评
2015年4月15日 14:08 | # | 引用
Diplodoc 说:
“Cloning into 'fortunes'...
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.”
阮老师,为什么我在“sudo git clone [email protected]:ruanyf/fortunes.git”的时候会出这个错误提示呢?
2015年4月16日 06:33 | # | 引用
阮一峰 说:
@Diplodoc:
可能是因为你本机上的 ~/.ssh/id_rsa 公钥文件,与Github上保存的公钥文件不一致。
可以两者之中换掉一个试试。
2015年4月16日 07:43 | # | 引用
frank 说:
自己装个 cgwin 试下吧。
2015年4月16日 08:47 | # | 引用
ka1guo0 说:
@Diplodoc:
换成git clone https://github.com/ruanyf/fortunes.git也可以。:)
2015年4月16日 14:42 | # | 引用
况海斌 说:
mac用户:
sudo mv fortunes/data/* /usr/share/games/fortunes/
换成:
sudo mv fortunes/data/* /usr/local/Cellar/fortune/9708/share/games/fortunes/
9708是当前fortune的版本号。
2015年4月21日 16:34 | # | 引用
aweany 说:
2015年4月22日 10:19 | # | 引用
Yann 说:
Ubuntu 有 fortune-zh
2015年4月24日 11:49 | # | 引用
royaso 说:
这好像就是从fortune-zh里提取的tang300等文件啊!!还不如直接安装fortune-zh
2015年5月 2日 04:34 | # | 引用
Junyi 说:
我fork了阮老师制作的中文格言库,数据转换成了JSON,并基于LeanCloud提供的服务,制作了网络版的fortune:
http://fortune.avosapps.com
2015年12月 4日 17:16 | # | 引用
passby 说:
蚂蚁搬动泰山,毫无感觉
2016年3月15日 21:19 | # | 引用
Amor 说:
在这个基础上发现了新玩法
fortune 20% chinese 20% tang300 20% song100 20% joke 20% love | cowsay -f $(ls /usr/local/Cellar/cowsay/3.04/share/cows/*.cow | gshuf -n1) | lolcat
https://olb7pgc09.qnssl.com/20170721150060552690999.png
2017年7月21日 10:54 | # | 引用
kfk 说:
我执行这两条命令都报这个 哎 我翻了好多地方 都没有找到
fortune -e fortunes chinese tang300 song100
fortune: no fortune found
2020年3月13日 17:27 | # | 引用
sequix 说:
有没有 firefox 的 fortune 扩展,每次打开 empty tab 时展示不同的 fortune,类似这个:
https://addons.mozilla.org/en-US/firefox/addon/mainichi/
2021年3月30日 13:28 | # | 引用
dcai 说:
m1的mac默认情况下无法写入/usr/share/games/fortunes/
如果用brew安装fortune的话可以把文件拷贝到:
cp fortunes/data/* /opt/homebrew/share/games/fortunes/
2022年1月30日 11:06 | # | 引用
Connor Zhang 说:
我用的 macos 12.3 系统
macos 已经无法向 /usr/share 中新建文件夹了
homebrew 安装后的位置也已经不在 /usr/local/bin 了
目前新的位置是:
/opt/homebrew/bin
所以如果是 macos 12.3 的话,应该将
sudo mv fortunes/data/* /usr/share/games/fortunes/
改为
mv fortunes/data/* /opt/homebrew/share/games/fortunes/
2022年4月 4日 23:14 | # | 引用
陈嘉琨 说:
[email protected]: Permission denied (publickey).
2022年9月 7日 17:16 | # | 引用
Tidus 说:
遇到个问题。。系统:
CentOS Linux release 7.6.1810 (Core)
安装的fortune:
https://rhel.pkgs.org/7/epel-x86_64/fortune-mod-1.99.1-17.el7.x86_64.rpm.html
fortune 是输出整个文件的。。 strfile 提示的是
"chinese.dat" created
There was 1 string
Longest string: 2470287 bytes
Shortest string: 2470287 bytes
貌似是因为 chinese 文件是 utf-8编码的缘故。。。windows下生成了一个ansi 编码格式的传上去是能正确解析的。
有啥解决办法吗?
2023年1月 7日 13:29 | # | 引用