- 论坛徽章:
- 0
|
本帖最后由 lys5300 于 2011-05-26 22:59 编辑
那天看同学在终端上查单词觉得很好,像他资讯了一下就决定自己用bash写一个。 :em02
也厚着脸皮的发在这里来与大家交流一下。 :em06
抛砖引玉 :em11 。
再一次修改。现在可以说已经很全面了。- #!/bin/bash
- # This is a dictionary based for Termial.
- # Its cache directory is in /tmp/dictionary/
- PS3='Your choice is : '
- word="$*"
- cache=~/.ydictionary/"$word"
- # read this word
- read_word()
- {
- re_word="$1"
- espeak -s 130 "$re_word" 1>/dev/null 2>&1
- exit 0
- }
- # judge whether read this word.
- judge_read()
- {
- readword="$1"
- clew="Read it?(y/n)y:"
- echo -ne "\033[35;1m$clew\033[00m"
- read answer
- : ${answer:=y}
- if [ "$answer" = "y" ];then
- read_word "$readword"
- else
- exit 5
- fi
- }
- if [ $# -lt 1 ];then
- echo
- echo "usag: `basename $0` [word]"
- echo
- exit 1
- fi
- if [ -e "$cache" ];then
- word2=$(cat "$cache")
- echo -e "\033[34;1m$word2\033[0m"
- echo
- judge_read "$word"
- exit 0
- elif [ ! -e ${cache%/*} ];then
- mkdir -p ${cache%/*}
- fi
- # 抓取网页快照。
- wordf=$(wget -q "http://dict.cn/ws.php?utf8=true&q=$word" -O -)
- # 截取指定字符段。
- # 判断是否有单词匹配,如果没有给出相近的。否则就进行查询。
- if echo "$wordf"|grep 'sugg' 1>/dev/null 2>&1
- then
- echo "You may want to search these words."
- sword=$(echo "$wordf"|sed -e 's/<[\/]*sugg>//g'|grep '^[^<]'|tr ["\n"] ["\t"])
- select guessw in $sword ;do
- if [ $guessw ];then
- echo -e "\033[36;1m$guessw\033[0m"
- ydic $guessw
- exit 2
- else
- read -n 1 -p "Do you want to try again:(y/n)" choice
- echo
- if [ "$choice" = "y" ] || [ "$choice" = "Y" ];then
- echo 'Please input again:'
- else
- exit 3
- fi
- fi
- done
- else
- word1=$(echo "$wordf"|sed -e 's/<[\/]*\(def\|sent\|orig\|trans\)>//g' -e 's/<em>\(.*\)<\/em>/( \1 )/g' -e 's/\(>\|<\)/ /g'|grep '^[^<]')
- echo "$word1">"$cache"
- echo -e "\033[32;1m$word1\033[0m"
- echo
- judge_read "$word"
- judge=$(cat "$cache")
- if [ "$judge" == "Not Found" ];then
- rm -f "$cache"
- fi
- #cache如果大于一百兆,提示用户。进行释放。
- total=$(du ${cache%/*}|cut -d/ -f 1)
- if [ $total -gt 102400 ];then
- echo
- echo -e "\tThe dictionary cache is beyond 100M.Maybe you can release some space."
- fi
- fi
- exit 0
复制代码 |
|