有爱的小脚本:启动终端时显示一句箴言

Filed Under (新长征路上的代码) by 逆铭 on 2009-11-22 18:46

效果就是每次启动终端时都有一个小动物什么的讲一句有意思的话:doubanclaim469c1764e4db1ecf

cowsay-fortune

说话的东西和说的话都随机出现。这个效果是我在Linux Mint里面看到的,感觉很有爱。下周考Unix环境编程,周末恶补一下,顺便写这个小脚本练手。

以我在用的 ubuntu 为例。首先确保安装 fortunescowsay 两个包。前者用于显示各种各样的趣味短句,后者则提供了一头会说话的奶牛(和其它各种诡异的东西)。关于fortunes还有一些有趣的包你可能也想一起安装,比如fortune-zh里有唐诗宋词,fortunes-ubuntu-server则有关于使用Ubuntu Server的贴士,等等。

新建一个文件cowsay-fortune,把以下代码复制进去:

#!/bin/bash
# Cow randomly says a hopefully interesting adage

# Get a short message from fortune, both offensive and not.
# Remove -a if you don't want to see offensive ones.
# Remove -s if you don't mind reading the long messages.
msg=`fortune -a -s`

# Randomly pick a mode of the cow
modes=("" -b -d -g -p -s -t -w -y ); mode=${modes[$(($RANDOM % 9))]}

# cowsay or cowthink?
cowdos=(cowsay cowthink); cowdo=${cowdos[$(($RANDOM % 2))]}

# Radomly pick a cow picture file
speaker=`cowsay -l | sed '1d;s/ /\n/g'| sort -R | head -1`

# That's it ^^
echo "$msg" | $cowdo -n -f $speaker $mode

保存后加上执行权限:

chmod +x cowsay-fortune

然后把这个文件复制到/usr/bin下

sudo cp cowsay-fortune /usr/bin

最后打开/etc/bash.bashrc

sudo gedit /etc/bash.bashrc

并在最后加上一行:

cowsay-fortune

保存后打开终端,应该就是这个效果了:

cowsay-fortune

顺便抱怨一下,shell编程时对空格的要求也太诡异了吧。。一会儿要求空,一会儿要求不空,一会儿又不限制。。。



Tags:,

Related Posts

Comments (1)

  1. cowsay
    用过,很有意思的东东

Post a comment

To submit your comment, click the image below where it asks you to...                  Clickcha - The One-click Captcha
Get Adobe Flash playerPlugin by wpburn.com wordpress themes

Search:

Codes, Notes & Scribbles Rss