我扔出砖。。。。(一个终端下的在线辞典)

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
Lys5300
帖子: 55
注册时间: 2010-08-27 15:49
来自: [email protected]
联系:

我扔出砖。。。。(一个终端下的在线辞典)

#1

帖子 Lys5300 » 2011-04-16 11:51

那天看同学在终端上查单词觉得很好,像他资讯了一下就决定自己用bash写一个。 :em02
也厚着脸皮的发在这里来与大家交流一下。 :em06
抛砖引玉 :em11
现在的问题是wget会产生一个过度文件,我觉得这样会影响速度。望高手指教。 :em11

修改了一下。让它多了几个功能。 :em01


代码: 全选

#!/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)y:" choice
		: ${choice:=y}
		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
上次由 Lys5300 在 2011-05-29 21:28,总共编辑 3 次。
在linux的路上跋涉。 云端的生活。

在学习的路途上,我永远是一个菜鸟。。。。。
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: 我扔出砖。。。。(一个终端下的在线辞典)

#2

帖子 枫叶饭团 » 2011-04-16 11:57

很好很强大 :em05 :em05
only_one
帖子: 186
注册时间: 2010-05-25 16:49

Re: 我扔出砖。。。。(一个终端下的在线辞典)

#3

帖子 only_one » 2011-04-16 12:14

wget 可以输出到标准输出
get_html=$(wget -q "http://dict.cn/ws.php?utf8=true&q=$word" -O -)
头像
Lys5300
帖子: 55
注册时间: 2010-08-27 15:49
来自: [email protected]
联系:

Re: 我扔出砖。。。。(一个终端下的在线辞典)

#4

帖子 Lys5300 » 2011-04-16 12:32

枫叶饭团 写了:很好很强大 :em05 :em05
有时候查会有点慢。希望能完善它。 :em06
在linux的路上跋涉。 云端的生活。

在学习的路途上,我永远是一个菜鸟。。。。。
头像
Lys5300
帖子: 55
注册时间: 2010-08-27 15:49
来自: [email protected]
联系:

Re: 我扔出砖。。。。(一个终端下的在线辞典)

#5

帖子 Lys5300 » 2011-04-16 12:32

only_one 写了:wget 可以输出到标准输出
get_html=$(wget -q "http://dict.cn/ws.php?utf8=true&q=$word" -O -)
嗯。我改改看看。
在linux的路上跋涉。 云端的生活。

在学习的路途上,我永远是一个菜鸟。。。。。
头像
Ubuntu与Linux
帖子: 1211
注册时间: 2010-06-09 19:57

Re: 我扔出砖。。。。(一个终端下的在线辞典)

#6

帖子 Ubuntu与Linux » 2011-04-16 14:44

代码: 全选

#!/bin/bash
if [ ! -s ~/Documents/Dic/$1 ]; then
w3m -no-cookie -dump http://dict.baidu.com/s?wd=$1|sed "/浠ヤ笅缁撴灉/,/浠ヤ笅缁撴灉/p" -n|sed -e '/浠ヤ笅缁撴灉/d'>> ~/Documents//Dic/$1
fi
clear
echo -e "\033[36m $1 :        \033[0m"
cat ~/Documents/Dic/$1
这是我的,写的时候不是乱码,现在变乱码了 :em06

PS,啥叫拓展名是禁止的阿
附件
dic (copy).txt
(258 Bytes) 已下载 20 次
头像
Lys5300
帖子: 55
注册时间: 2010-08-27 15:49
来自: [email protected]
联系:

Re: 我扔出砖。。。。(一个终端下的在线辞典)

#7

帖子 Lys5300 » 2011-04-17 7:36

Ubuntu与Linux 写了:

代码: 全选

#!/bin/bash
if [ ! -s ~/Documents/Dic/$1 ]; then
w3m -no-cookie -dump http://dict.baidu.com/s?wd=$1|sed "/浠ヤ笅缁撴灉/,/浠ヤ笅缁撴灉/p" -n|sed -e '/浠ヤ笅缁撴灉/d'>> ~/Documents//Dic/$1
fi
clear
echo -e "\033[36m $1 :        \033[0m"
cat ~/Documents/Dic/$1
这是我的,写的时候不是乱码,现在变乱码了 :em06

PS,啥叫拓展名是禁止的阿

抱歉阿。我也不明了。。。 :em06
在linux的路上跋涉。 云端的生活。

在学习的路途上,我永远是一个菜鸟。。。。。
头像
steven0lisa
帖子: 68
注册时间: 2008-01-31 11:57

Re: 我扔出砖。。。。(一个终端下的在线辞典)

#8

帖子 steven0lisa » 2011-05-29 1:51

看看我这篇吧。 已经写的比较完整了。
viewtopic.php?f=95&t=310153 :em09

呵呵,抛的不错
头像
HuntXu
帖子: 5776
注册时间: 2007-09-29 3:09

Re: 我扔出砖。。。。(一个终端下的在线辞典)

#9

帖子 HuntXu » 2011-05-29 11:20

viewtopic.php?f=73&t=249262
改改应该还能用
HUNT Unfortunately No Talent...
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 我扔出砖。。。。(一个终端下的在线辞典)

#10

帖子 eexpress » 2011-05-29 15:51

g-translate.pl 也可以算在线。
● 鸣学
回复