Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120445
  • 博文数量: 31
  • 博客积分: 691
  • 博客等级: 中士
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-16 16:45
文章分类

全部博文(31)

文章存档

2012年(4)

2011年(27)

分类: LINUX

2011-05-26 22:32:23

方便在终端下查单词,经过多次的修改,现在已经算比较完善了。其功能:

1,查单词(废话)

2,相关例句

3,建立缓存文件

4,读单词

5,进行模糊匹配

  1. #!/bin/bash
  2. # This is a dictionary based for Termial.
  3. # Its cache directory is in ~/.ydictionary/
  4. PS3='Your choice is : '
  5. word="$*"
  6. cache=~/.ydictionary/"$word"
  7. # read this word
  8. read_word()
  9. {
  10. re_word="$1"
  11. mplayer "" >/dev/null 2>&1
  12. if [ $? ]
  13. then
  14. :
  15. else
  16. espeak -s 130 "$re_word" 1>/dev/null 2>&1
  17. fi
  18. exit 0
  19. }
  20. # judge whether read this word.
  21. judge_read()
  22. {
  23. readword="$1"
  24. clew="Read it?(y/n)y:"
  25. echo -ne "\033[35;1m$clew\033[00m"
  26. read answer
  27. : ${answer:=y}
  28. if [ "$answer" = "y" ];then
  29. read_word "$readword"
  30. else
  31. exit 5
  32. fi
  33. }
  34. if [ $# -lt 1 ];then
  35. echo
  36. echo "usag: `basename $0` [word]"
  37. echo
  38. exit 1
  39. fi
  40. if [ -e "$cache" ];then
  41. word2=$(cat "$cache")
  42. echo -e "\033[34;1m$word2\033[0m"
  43. echo
  44. judge_read "$word"
  45. exit 0
  46. elif [ ! -e ${cache%/*} ];then
  47. mkdir -p ${cache%/*}
  48. fi

  49. # 抓取网页快照。
  50. wordf=$(wget -q "" -O -)

  51. # 截取指定字符段。
  52. # 判断是否有单词匹配,如果没有给出相近的。否则就进行查询。
  53. if echo "$wordf"|grep 'sugg' 1>/dev/null 2>&1
  54. then
  55. echo "You may want to search these words."
  56. sword=$(echo "$wordf"|sed -e 's/<[\/]*sugg>//g'|grep '^[^<]'|tr ["\n"] ["\t"])
  57. select guessw in $sword ;do
  58. if [ $guessw ];then
  59. echo -e "\033[36;1m$guessw\033[0m"
  60. ydic $guessw
  61. exit 2
  62. else
  63. read -n 1 -p "Do you want to try again:(y/n)y:" choice
  64. : ${choice:=y}
  65. echo
  66. if [ "$choice" = "y" ] || [ "$choice" = "Y" ];then
  67. echo 'Please input again:'
  68. else
  69. exit 3
  70. fi
  71. fi
  72. done
  73. else
  74. word1=$(echo "$wordf"|sed -e 's/<[\/]*\(def\|sent\|orig\|trans\)>//g' -e 's/<em>\(.*\)<\/em>/( \1 )/g' -e 's/\(>\|<\)/ /g'|grep '^[^<]')
  75. echo "$word1">"$cache"
  76. echo -e "\033[32;1m$word1\033[0m"
  77. echo
  78. judge_read "$word"
  79. judge=$(cat "$cache")
  80. if [ "$judge" == "Not Found" ];then
  81. rm -f "$cache"
  82. fi
  83. #cache如果大于一百兆,提示用户。进行释放。
  84. total=$(du ${cache%/*}|cut -d/ -f 1)
  85. if [ $total -gt 102400 ];then
  86. echo
  87. echo -e "\tThe dictionary cache is beyond 100M.Maybe you can release some space."
  88. fi
  89. fi
  90. exit 0

阅读(1558) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~