fortune 命令简介

作者: 阮一峰

日期: 2015年4月14日

美国中餐馆的最后一道菜,往往是小甜饼,叫做"幸运饼"(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/ 。

(完)

留言(32条)

debian下很早就有这个中午包了,全是唐诗宋词,fortune-zh
ps: 我用这个来做git 提交的commit

英文的可以配合toilet耍耍。

谢谢阮老师,很棒,已经换上

fortune chinese 为何输出整个文件,而不是一段话呢?

❯ fortune -v
fortune-mod version 9708

fortune-mod上游已死- -

/etc/profile.d/fortune.sh


#!/bin/sh

case $- in
*i* ) echo;fortune ascii-art computers debian fortunes linux linuxcookie science;echo;;
esac

不知道为什么

$ fortune -e fortunes chinese tang300 song100

# 或者

$ fortune 25% fortunes 25% chinese 25% tang300 25% song100

无法生效,也不报错,它还是按照默认比例。没办法,只能把所有 .dat 文件删掉,只生成指定几个库的索引,比例无法指定。

引用frank的发言:

fortune chinese为何输出整个文件,而不是一段话呢?

❯ fortune -v
fortune-mod version 9708

+1 中文输出的是整个文件 OS X

@yannisxu,@frank,

我没有Mac,能够运行一下strfile,告诉我结果吗?

可以在fortune/data目录中,运行下面命令。

$ strfile song100

@吾爱:

fortune命令后直接跟格言包文件名,就可以了。

$ fortune tang300

/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

好像 strfile 一下就可以了耶!

Mac 下用 brew install fortune 安装,安装后 ~/.zlogin 自动会添加 fortune -a 命令,改成你的命令即可。
然后把你的每个数据文件 strfile 一下,就 OK 了。

没有windows差评

“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”的时候会出这个错误提示呢?

@Diplodoc:

可能是因为你本机上的 ~/.ssh/id_rsa 公钥文件,与Github上保存的公钥文件不一致。

可以两者之中换掉一个试试。

引用fadeoc的发言:

没有windows差评

自己装个 cgwin 试下吧。

mac用户:
sudo mv fortunes/data/* /usr/share/games/fortunes/
换成:
sudo mv fortunes/data/* /usr/local/Cellar/fortune/9708/share/games/fortunes/

9708是当前fortune的版本号。

引用阮一峰的发言:

@Diplodoc:

可能是因为你本机上的 ~/.ssh/id_rsa 公钥文件,与Github上保存的公钥文件不一致。

可以两者之中换掉一个试试。

Ubuntu 有 fortune-zh

引用Yann的发言:

Ubuntu 有 fortune-zh

这好像就是从fortune-zh里提取的tang300等文件啊!!还不如直接安装fortune-zh

我fork了阮老师制作的中文格言库,数据转换成了JSON,并基于LeanCloud提供的服务,制作了网络版的fortune:
http://fortune.avosapps.com

蚂蚁搬动泰山,毫无感觉

在这个基础上发现了新玩法
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

我执行这两条命令都报这个 哎 我翻了好多地方 都没有找到
fortune -e fortunes chinese tang300 song100
fortune: no fortune found

有没有 firefox 的 fortune 扩展,每次打开 empty tab 时展示不同的 fortune,类似这个:

https://addons.mozilla.org/en-US/firefox/addon/mainichi/

m1的mac默认情况下无法写入/usr/share/games/fortunes/
如果用brew安装fortune的话可以把文件拷贝到:


cp fortunes/data/* /opt/homebrew/share/games/fortunes/

我用的 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/

[email protected]: Permission denied (publickey).

遇到个问题。。系统:
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 编码格式的传上去是能正确解析的。
有啥解决办法吗?

老师,我想让这个开机打开终端显示一句,该怎么实现?

我要发表看法

«-必填

«-必填,不公开

«-我信任你,不会填写广告链接