一个终端下的在线辞典(bash)

1593阅读 0评论2011-05-26 lys5300
分类:LINUX

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

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

上一篇:Linux下查看文件编码,转换编码
下一篇:菜鸟的Ubuntu私房菜