-
Notifications
You must be signed in to change notification settings - Fork 4
/
record.sh
executable file
·100 lines (91 loc) · 2.46 KB
/
record.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
yarn='\033[0;34m'
pulper='\033[0;35m'
blue='\033[0;36m'
white='\033[0;37m'
end='\033[0m'
configFile='/home/kcp/.config/app-conf/key-record/main.conf'
help(){
printf "Run:$red sh record.sh$green <verb> $yellow<args>$end\n"
format=" $green%-5s $yellow%-15s$end%-20s\n"
printf "$format" "-h" "" "帮助"
printf "$format" "-q" "" "搜索键盘设备可能对应的事件号"
printf "$format" "-b" "" "显示正在后台运行的记录脚本"
printf "$format" "-s" "<eventNum>" "root: 使用指定的事件号/上次配置启动记录脚本"
printf "$format" "-d" "" "root: 停止所有正在后台运行的记录脚本"
}
startup(){
if [ $(id -u) != "0" ]; then
printf $red"Please use root to run this script\n"$end
exit 1
fi
if [ ! -f $configFile ]; then
echo "[event]\nkey = 9\n\n[redis]\nhost = 127.0.0.1\nport = 6666\ndb = 2\npassword=\n" >> $configFile
fi
eventNum=$1
path=$(cd `dirname $0`; pwd)
if [ $eventNum'z' = 'z' ];then
printf "$green use default config to start $end \n"
else
sed -i "s/key\s=.*/key = $eventNum/g" $configFile
printf "$green use event $eventNum to start $end \n"
fi
(python3 $path/RecordClickWithRedis.py &)
}
shutdown(){
if [ $(id -u) != "0" ]; then
printf $red"Please use root to run this script\n"$end
exit 1
fi
id=`ps -ef | grep "WithRedis.py" | grep -v "grep" | grep -v "\-d" | awk '{print $2}'`
if [ "${id}1" = "1" ];then
printf $red"not exist background running script\n"$end
else
kill -9 $id
fi
}
query(){
result=$(cat /proc/bus/input/devices)
flag=0
printf $green"It could be a keyboard event: "$end
for line in $result;do
# echo $line
result=`echo $line | grep event`
if [ $flag = 1 ] && [ $result'z' != 'z' ];then
printf $line" "
fi
if [ $line = 'kbd' ];then
flag=1
fi
# 一个设备可能占据多行, 具有多个 event
first=$(echo $line | grep "Bus")
if [ $first'z' != 'z' ]; then
flag=0
fi
done
echo ""
}
case $1 in
-h)
help ;;
-s) # start
startup $2
;;
-q)
query
;;
-d)
shutdown
;;
-b) # background
ps -ef | grep "WithRedis.py" | grep -v "grep"
;;
-cnf)
vim $configFile
;;
*)
help
;;
esac