第三章:Creating Utilities--27.增加一个本地词典

    这是从博客园移过来的最后一篇,确实很麻烦呀。

   做完了上面的第25、26个脚本后,我们想要自己手动增加一个本地词典,这样就不用在每次遇到一个新的单词后,都会一遍遍的报错了。

代码:

#!/bin/sh
 
 # spelldict.sh -- 使用'aspell'特性以及一些过滤以便
 # 允许命令行拼写检查给定的输入文件
 
 # 不可避免的,你会发现,有些词汇是错误的,但你认为
 # 它们是正确的。简单的将它们保存在一个文件中,一次
 # 一行,并且确定变量'okaywords'指向这个文件。
 
 okaywords="$HOME/okaywords"
 tempout="/tmp/spell.tmp.$$"
 spell="aspell"    # 根据需要修改
 
 trap "/bin/rm -f $tempout" EXIT
 
 if [ -z "$1" ]; then
     echo "Usage: spell file | URL" >&2
     exit 1
 elif [ ! -f $okaywords ]; then
     echo "No personal dictionary found. Create one and return this command." >&2
     echo "Your dictionary file: $okaywords" >&2
     exit 1
 fi
 
 for filename
 do
     $spell -a < $filename | \
     grep -v '@(#)' | sed "s/\'//g" | \
         awk '{if(length($0) > 15 && length($2) > 2) print $2}' | \
     grep -vif $okaywords | \
     grep '[[:lower:]]' | grep -v '[[:digit:]]' | sort -u | \
     sed 's/^/ /' > $tempout
 
     if [ -s $tempout ]; then
         sed 's/^/${filename}: /' $tempout
     fi
 done
 
 exit 0

运行脚本:
这个脚本需要在命令行上提供一个或多个文件名

运行结果:

首先,一个空的个人字典,txt内容摘录自爱丽丝漫游记:
$ spelldict ragged.txt 
ragged.txt:    herrself 
ragged.txt:    teacups 
ragged.txt:    Gryphon 
ragged.txt:    clamour 

有两个词拼错了,所以准备用echo命令把它们加到okaywords文件中:

$ echo "Gryphon" >> ~/.okaywords 
$ echo "teacups" >> ~/.okaywords 

扩展了词典文件后的检查结果如下:

$ spelldict ragged.txt 
ragged.txt:    herrself 
ragged.txt:    clamour
ps: 这3章的单词检查,对我个人而言真的很无趣。期待后续的精彩脚本吧。


你可能感兴趣的:(linux,shell,bash)