第三章:Creating Utilities--27.增加一个本地词典
做完了上面的第25、26个脚本后,我们想要自己手动增加一个本地词典,这样就不用在每次遇到一个新的单词后,都会一遍遍的报错了。
代码:
1 #!/bin/sh 2 3 # spelldict.sh -- 使用'aspell'特性以及一些过滤以便 4 # 允许命令行拼写检查给定的输入文件 5 6 # 不可避免的,你会发现,有些词汇是错误的,但你认为 7 # 它们是正确的。简单的将它们保存在一个文件中,一次 8 # 一行,并且确定变量'okaywords'指向这个文件。 9 10 okaywords="$HOME/okaywords" 11 tempout="/tmp/spell.tmp.$$" 12 spell="aspell" # 根据需要修改 13 14 trap "/bin/rm -f $tempout" EXIT 15 16 if [ -z "$1" ]; then 17 echo "Usage: spell file | URL" >&2 18 exit 1 19 elif [ ! -f $okaywords ]; then 20 echo "No personal dictionary found. Create one and return this command." >&2 21 echo "Your dictionary file: $okaywords" >&2 22 exit 1 23 fi 24 25 for filename 26 do 27 $spell -a < $filename | \ 28 grep -v '@(#)' | sed "s/\'//g" | \ 29 awk '{if(length($0) > 15 && length($2) > 2) print $2}' | \ 30 grep -vif $okaywords | \ 31 grep '[[:lower:]]' | grep -v '[[:digit:]]' | sort -u | \ 32 sed 's/^/ /' > $tempout 33 34 if [ -s $tempout ]; then 35 sed 's/^/${filename}: /' $tempout 36 fi 37 done 38 39 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章的单词检查,对我个人而言真的很无趣。期待后续的精彩脚本吧。