基于fortune+cowsay+lolcat組合拳的終端美化
這里通過(guò)構(gòu)建fortune、cowsay、lolcat組合拳對(duì)Mac終端進(jìn)行美化
abstract.pngfortune
該命令每次執(zhí)行時(shí),會(huì)隨機(jī)輸出一句人生格言
# 安裝
brew install fortune
# 查看版本信息,確認(rèn)安裝成功
fortune -v
figure 1.jpg
只不過(guò)fortune默認(rèn)的格言庫(kù)是英文的。這里我們期望能夠輸出中國(guó)的古詩(shī)詞之類的
## 下載詩(shī)詞文件
git clone https://github.com/ruanyf/fortunes.git
# 生成索引文件
cd ./fortunes/data
strfile fortunes && strfile chinese && strfile tang300 && strfile song100
figure 2.jpg
# 確認(rèn) games/fortunes 文件夾位置
brew list fortune | grep -E "games/fortunes/" | head -n 1
# 將詩(shī)詞文件data目錄下的內(nèi)容全部移動(dòng)到 games/fortunes 文件夾當(dāng)中
sudo mv fortunes/data/* /usr/local/Cellar/fortune/9708/share/games/fortunes/
figure 3.jpg
# 執(zhí)行fortune命令,并對(duì)每個(gè)庫(kù)設(shè)置比例,進(jìn)行隨機(jī)抽取
fortune 25% fortunes 25% chinese 25% tang300 25% song100
figure 4.jpgcowsay
該命令接受一個(gè)文本,然后通過(guò)動(dòng)物說(shuō)話的圖形進(jìn)行展示
# 安裝
brew install cowsay
用法如下
cowsay "Good"
# 查看可用動(dòng)物
cowsay -l
# 通過(guò)-f選項(xiàng)指定動(dòng)物
cowsay -f cheese "Good"
figure 5.jpglolcat
該命令可以對(duì)命令行對(duì)輸出產(chǎn)生彩虹的顏色效果
# 安裝
brew install lolcat
效果如下
ll ~ | lolcat
figure 6.jpg組合拳
現(xiàn)在我們來(lái)編寫一個(gè)Shell腳本。實(shí)現(xiàn)啟動(dòng)終端時(shí),會(huì)隨機(jī)選擇一種動(dòng)物隨機(jī)輸出一條彩虹效果的格言。其中,cowsay命令的配置文件路徑可通過(guò)cowsay -l命令獲取
figure 7.jpg
腳本文件cowSayFortuneBylolcat.sh如下所示
#!/usr/local/bin/zsh
# 打開(kāi)終端時(shí),隨機(jī)選擇一種動(dòng)物隨機(jī)輸出一條彩虹效果的格言
# cowsay命令配置文件路徑
cowsay_config_path=/usr/local/Cellar/cowsay/3.04_1/share/cows
# 先篩選出配置路徑下.cow類型的所有文件。
# 然后通過(guò)wc命令的-l選項(xiàng)統(tǒng)計(jì)行數(shù),即.cow類型文件的數(shù)量
animal_counts=`ls ${cowsay_config_path} | grep ".cow$" | wc -l`
# 生成一個(gè)在[1,cow_counts]范圍內(nèi)的隨機(jī)數(shù)
rand_num=`jot -r 1 1 ${animal_counts}`
# 選擇第rand_num個(gè).cow類型文件
animal=`ls ${cowsay_config_path} | grep ".cow$" | sort | sed -n "${rand_num}p"`
# 輸出格言
fortune 10% fortunes 30% chinese 30% tang300 30% song100 | cowsay -f ${animal} | lolcat
現(xiàn)在對(duì)該腳本執(zhí)行權(quán)限
# 對(duì)腳本賦予執(zhí)行權(quán)限
chmod u+x cowSayFortuneBylolcat.sh
figure 8.jpg
這里我們使用的Shell為zsh。故在用戶目錄下的.zshrc文件中添加執(zhí)行該腳本的命令
...
# 執(zhí)行cowSayFortuneBylolcat腳本。打開(kāi)終端時(shí),隨機(jī)使用一種動(dòng)物隨機(jī)輸出一條彩虹效果的格言
~/CustomShellScript/cowSayFortuneBylolcat.sh
...
figure 9.jpg
評(píng)論
圖片
表情
