本站内容有下面分类知识,欢迎您的到来^_^

shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言

设soyo123为首页 收藏本站
当前位置:|主页>shell相关E文资料>

#25 Checking the Spelling of Individual Words

百度收藏 QQ搜藏

High-end programs like StarOffice, OpenOffice.org, and Microsoft Word include built-in spell-checking software, but the more rudimentary commandline question of whether a single word is spelled correctly or not is beyond the ability of any of these applications.

Similarly, most Unixes include a spell-check package that works reasonably well, albeit with a crusty interface. Given an input file or data stream, the packages generate a long list of all possible misspellings. Some spell-check packages include interactive spell-check applications. Again, however, none of them offer a simple way to check the spelling of a single word.

Don't have a spell-check program installed?
For those Unix distributions that don't have a spell package — though, really, all of 'em should nowadays, with disk space so cheap — an excellent option is to install ispell, from http://fmg-www.cs.ucla.edu/geoff/ispell.html
 

The Code
#!/bin/sh

# checkspelling - Checks the spelling of a word.

spell="ispell -l"         # if you have ispell installed
                          # if not, just define spell=aspell or
                          # equivalent
if [ $# -lt 1 ] ; then
  echo "Usage: $0 word or words" >&2; exit 1
fi

for word
do
  if [ -z $(echo $word | $spell) ] ; then
    echo "$word:                spelled correctly."
  else
    echo "$word:                misspelled."
  fi
done

exit 0

Running the Script
To use this script, simply specify one or more words as arguments of the checkspelling command.

The Results
It's now easy to ascertain the correct spelling of "their":

$ checkspelling thier their
thier:          misspelled.
their:          spelled correctly.

Hacking the Script
There's quite a bit you can do with a spelling utility and, for that matter, quite a bit that ispell can already accomplish. This is just the tip of the proverbial iceberg, as you'll see in the next script.


上一篇:#24 An Interactive Calculator 下一篇:#26 Shpell: An Interactive Spell-Checking Facility
power by soyo123 2007-2008