[bash]
#!/bin/bash
help(){
echo -e "Usage: $0 [OPTIONS] STRING"
echo -e " OPTINOS:"
echo -e " -h: Show this help."
echo -e " -n: Use notify-send, use standard output by default."
echo -e " -s: Enalbe sound, use mplayer to play."
echo -e ""
}
error1(){
echo "No word attached!"
help
exit 0
}
[ -z "$1" ] && help && exit 1
for i in $@; do
if [ "x$i" != "x${i#-}" ]; then
[ "x$i" == "x-n" ] && NOTIFY=1
[ "x$i" == "x-h" ] && HELP=1
[ "x$i" == "x--help" ] && HELP=1
[ "x$i" == "x-s" ] && SOUND=1
else
WORD=$i
fi
done
[ "$HELP" == "1" ] && help && exit 1
[ -z "$WORD" ] && [ "x$NOTIFY" != "x1" ] && error1
[ -z "$WORD" ] && [ "x$NOTIFY" == "x1" ] && WORD=$(xsel)
WEBAPI="http://dict.cn/ws.php?utf8=true&q="
TMPFILE=/tmp/dict.tmp
wget "$WEBAPI""$WORD" -O $TMPFILE &> /dev/null
cache=$(cat $TMPFILE | sed '/^<?.*/d;/dict>$/d')
while IFS='<>' read _ starttag value endtag; do
case "$starttag" in
key) key+=("$value") ;;
lang) lang+=("$value");;
audio) audio+=("$value");;
pron) pron+=("$value");;
esac
done < $TMPFILE
if [ -n "$key" ]; then
if echo $pron | grep "&#" >/dev/null; then
pron=$(echo $pron | sed 's/ʒ/ʒ/g;s/ʌ/ʌ/g;s/ʃ/ʃ/g;s/ɛ/ɜ/g;s/ə/ə/g;s/ɔ/ɔ/g;s/ɑ/ɑ/g;s/ŋ/ŋ/g;s/ð/ð/g;s/æ/æ/g')
fi
sent=$(echo "$cache" | grep '<sent>' | sed 's/<sent>//g;s/<\/sent>//g;s/<orig>//g;s/<\/orig>//g;s/<trans>//g;s/<\/trans>//g;s/<em>//g;s/<\/em>//g')
#FIXME
cache1=$(echo "$cache" | sed -n '/<sent>/,/<\/sent>/!p')
def=$(echo "$cache1" | sed '/<def>/,/<\/def>/!d;s/<def>//;s/<\/def>//')
else
sugg=$(echo "$cache" | sed 's/<sugg>//g;s/<\/sugg>//g')
fi
if [ "x$NOTIFY" == "x1" ];then
[ -n "$key" ] && notify-send -i accessories-dictionary "$key" "[$pron]\n$def"
[ -z "$key" ] && notify-send -i dialog-warning "$WORD" "未找到!"
else
echo ""
if [ -n "$key" ]; then echo "======== $key ========"; else echo "$WORD Not found!"; fi
[ -n "$pron" ] && echo "[$pron]"
[ -n "$def" ] && echo "$def"
[ -n "$sent" ] && echo -e "\n$sent"
[ -n "$sugg" ] && echo "Maybe you mean:" $sugg
echo ""
fi
[ "x$SOUND" == "x1" ] && [ -n "$audio" ] && mplayer -really-quiet $audio &>/dev/null
[/bash]
效果:
代码: 全选
$./dict.sh game
======== game ========
[geim]
n.游戏, 运动会, 比赛
v.赌博
Who'll climb up to get it?' I'm game (to try).'`谁愿意爬上去把它拿下来?'`我来(试试).'
John only pretends to do his share of the work; he's just not playing the game.约翰只是装成做他那份工作,实际上并不真心好好干。
The little boy was shocked by the fall, but he was game enough to get up and try again.这个小男孩因摔倒而受到惊吓,但他勇敢地站起来准备再试。