-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.sh
224 lines (112 loc) · 4.35 KB
/
screen.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env bash
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
PLAIN='\033[0m'
red() {
echo -e "\033[31m\033[01m$1\033[0m"
}
green() {
echo -e "\033[32m\033[01m$1\033[0m"
}
yellow() {
echo -e "\033[33m\033[01m$1\033[0m"
}
REGEX=("debian" "ubuntu" "centos|red hat|kernel|oracle linux|alma|rocky" "'amazon linux'")
RELEASE=("Debian" "Ubuntu" "CentOS" "CentOS")
PACKAGE_UPDATE=("apt-get -y update" "apt-get -y update" "yum -y update" "yum -y update")
PACKAGE_INSTALL=("apt -y install" "apt -y install" "yum -y install" "yum -y install")
PACKAGE_UNINSTALL=("apt -y autoremove" "apt -y autoremove" "yum -y autoremove" "yum -y autoremove")
[[ $EUID -ne 0 ]] && red "请在root用户下运行脚本" && exit 1
CMD=("$(grep -i pretty_name /etc/os-release 2>/dev/null | cut -d \" -f2)" "$(hostnamectl 2>/dev/null | grep -i system | cut -d : -f2)" "$(lsb_release -sd 2>/dev/null)" "$(grep -i description /etc/lsb-release 2>/dev/null | cut -d \" -f2)" "$(grep . /etc/redhat-release 2>/dev/null)" "$(grep . /etc/issue 2>/dev/null | cut -d \\ -f1 | sed '/^[ ]*$/d')")
for i in "${CMD[@]}"; do
SYS="$i" && [[ -n $SYS ]] && break
done
for ((int = 0; int < ${#REGEX[@]}; int++)); do
[[ $(echo "$SYS" | tr '[:upper:]' '[:lower:]') =~ ${REGEX[int]} ]] && SYSTEM="${RELEASE[int]}" && [[ -n $SYSTEM ]] && break
done
[[ -z $SYSTEM ]] && red "不支持当前VPS系统,请使用主流的操作系统" && exit 1
if [[ -z $(type -P screen) ]]; then
if [[ ! $SYSTEM == "CentOS" ]]; then
${PACKAGE_UPDATE[int]}
fi
${PACKAGE_INSTALL[int]} screen
fi
back2menu() {
echo ""
green "所选操作执行完成"
read -rp "请输入“y”退出,或按任意键回到主菜单:" back2menuInput
case "$back2menuInput" in
y) exit 1 ;;
*) menu ;;
esac
}
createScreen(){
read -rp "设置screen后台会话名称:" screenName
if [[ -z $screenName ]]; then
red "未设置screen后台会话名称,退出操作"
back2menu
fi
screen -U -S $screenName
back2menu
}
enterScreen(){
screenNames=`screen -ls | grep '(Detached)' | awk '{print $1}' | awk -F "." '{print $2}'`
if [[ -n $screenNames ]]; then
yellow "当前运行的Screen后台会话如下所示:"
green "$screenNames"
fi
read -rp "输入进入的screen后台会话名称:" screenName
screen -r $screenName || red "没有找到 $screenName 会话"
back2menu
}
deleteScreen(){
screenNames=`screen -ls | grep '(Detached)' | awk '{print $1}' | awk -F "." '{print $2}'`
if [[ -n $screenNames ]]; then
yellow "当前运行的Screen后台会话如下所示:"
green "$screenNames"
fi
read -rp "输入删除的screen后台会话名称:" screenName
screen -S $screenName -X quit || red "没有找到 $screenName 会话"
back2menu
}
killAllScreen(){
screenNames=`screen -ls | grep '(Detached)' | awk '{print $1}' | awk -F "." '{print $2}'`
screen -wipe
[[ -n $screenNames ]] && screen -ls | grep '(Detached)' | cut -d. -f1 | awk '{print $1}' | xargs kill
green "所有screen后台会话已清除完毕" || red "没有任何screen后台会话"
back2menu
}
menu(){
clear
echo "#############################################################"
echo -e "# ${RED} Screen 后台管理脚本${PLAIN} #"
echo "#############################################################"
echo ""
echo -e " ${GREEN}1.${PLAIN} 创建screen后台会话并设置会话名称"
echo " -------------"
echo -e " ${GREEN}2.${PLAIN} 查看并进入指定screen后台会话"
echo -e " ${GREEN}3.${PLAIN} 查看并删除指定screen后台会话"
echo " -------------"
echo -e " ${GREEN}4.${PLAIN} 清除所有screen后台会话"
echo " -------------"
echo -e " ${GREEN}0.${PLAIN} 退出脚本"
echo ""
yellow "使用脚本的一些小提示:"
yellow "1. 退出Screen后台会话时,请按Ctrl+A+D快捷键退出"
yellow "2. 请谨慎使用4选项"
echo ""
read -rp "请输入选项 [0-4]:" menuNumberInput
case "$menuNumberInput" in
1 ) createScreen ;;
2 ) enterScreen ;;
3 ) deleteScreen ;;
4 ) killAllScreen ;;
* ) exit 1 ;;
esac
}
if [ $# > 0 ]; then
menu
else
menu
fi