首页 > Script > 翻箱淘到的在线翻译脚本

翻箱淘到的在线翻译脚本

[总点击:219次]
2009年12月28日

偶尔清理我的个人可执行目录($HOME/.bin)发现了远古时代写的一个翻译脚本,牵出来遛了一遛,还可以用哈,嘿嘿,顺手给改了几下,添加了以下一些特性:

1) deamon 化(直接进后台了,不必老开着一个终端跑)
2) 三种显示方式:kdialog,notify-send 和 gnome-osd
3) 脚本名加 -k 参数剋掉后台进程

露脸一下:
dic_sh

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
 
# Name:     dic.sh
# Author:   xiooli <xioooli[at]yahoo.com.cn>
# Site:     http://joolix.com
# Licence:  GPLv3
# Version:  091228
 
timeout=10
 
trans() {
	str="`xsel`"
	while true ; do
	  a="`xsel`"
	  if [ "$a" != "$str" ] ; then
		str="$a"
		[ ${#a} -lt 15 ] && word="`w3m -dump -no-cookie http://dict.cn/mini.php?q="$a" \
		| sed -e '$d' -e 's/</ /g' -e 's/>/ /g'`"
		if [ "`which gnome-osd-client 2>/dev/null`" ]; then
			gnome-osd-client -f "<message id='word' osd_fake_translucent_bg='off' \
			osd_vposition='top' osd_halignment='right' animations='on' hide_timeout='10000'>\
			<span size='15000' foreground='pink'>`echo "$word"`</span></message>"
		elif [ "`which kdialog 2>/dev/null`" ]; then
			kdialog --passivepopup "$word" "${timeout:-5}" &>/dev/null
		elif [ "`which notify-send 2>/dev/null`" ]; then
			timeout=${timeout:-5}
			timeout=$((timeout*1000))
			notify-send -t "$timeout" "$word"
		fi
	  fi
	  sleep 1
	done
}
 
# daemonize
if [ "x$1" != "x--" -a "x$1" != "x-k"  -a "x$1" != "x-h" ]; then
	if [ ! "`cat /tmp/dic.pid 2>/dev/null`" ]; then
		"$0" -- 1> /dev/null 2> /dev/null &
		exit 0
	else
		echo "dic is running."
		exit 0
	fi
elif [ "x$1" = "x-k" ]; then
	pic_pid="`cat /tmp/dic.pid 2>/dev/null`"
	if [ "$pic_pid" ]; then
		kill -9 "$pic_pid" 2>/dev/null
		rm /tmp/dic.pid 2>/dev/null
		echo "dic process is terminated."
	else
		echo "no dic process is running."
	fi
	exit 0
elif [ "x$1" = "x-h" ]; then
	nm="`basename $0`"
	echo -e "$nm: run\n$nm -k: kill daemon\n$nm -h: show help"
	exit 0
fi
 
echo $$ > /tmp/dic.pid
trans

Script , , ,

本文的评论功能被关闭了.