分享Shell模板

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
#!/bin/bash
rsname=Template #shell名称
rsver=1.0 #shell版本号
base_dir=/usr/local #运行初始目录
mirrorLink=https://www.idiyrom.com #下载地址
flag=$1 #参数

#格式化显示函数
Echo()
{
case $1 in
success) flag="\033[1;32m"
;;
failure) flag="\033[1;31m"
;;
warning) flag="\033[1;33m"
;;
msg) flag="\033[1;34m"
;;
*) flag="\033[1;34m"
;;
esac
if [[ $LANG =~ [Uu][Tt][Ff] ]]
then
echo -e "${flag}${2}\033[0m"
else
echo -e "${flag}${2}\033[0m" iconv -f utf-8 -t gbk
fi
}
#调用方法
#绿色 Echo "success" "成功信息"
#蓝色 Echo "msg" "提示信息"
#红色 Echo "failure" "错误信息"
#黄色 Echo "warning" "警告信息"

Check_OS() {
if [ -n "$(grep 'Aliyun Linux release' /etc/issue)" -o -e /etc/redhat-release ]; then
OS=CentOS
[ -n "$(grep ' 7\.' /etc/redhat-release 2> /dev/null)" ] && CentOS_RHEL_version=7
[ -n "$(grep ' 6\.' /etc/redhat-release 2> /dev/null)" -o -n "$(grep 'Aliyun Linux release6 15' /etc/issue)" ] && CentOS_RHEL_version=6
[ -n "$(grep ' 5\.' /etc/redhat-release 2> /dev/null)" -o -n "$(grep 'Aliyun Linux release5' /etc/issue)" ] && CentOS_RHEL_version=5
elif [ -n "$(grep 'Amazon Linux AMI release' /etc/issue)" -o -e /etc/system-release ]; then
OS=CentOS
CentOS_RHEL_version=6
fi
}

#shell介绍信息
header()
{
printf "
###################################################################
# $rsname version $rsver Author: Ropon <idiyrom.com> #
# For more information please visit https://idiyrom.com/83.html #
#-----------------------------------------------------------------#
# Copyright @2017-2018 idiyrom.com. All rights reserved. #
###################################################################

"
}

#帮助提示
showhelp()
{
header
Echo "msg" "Usage: $rsname [OPTIONS]"
echo
Echo "msg" "OPTIONS:"
Echo "msg" "-h --help : Show help of $rsname"
Echo "msg" "-u --update : update Check for $rsname"
Echo "msg" "-i --install : install $rsname version $rsver to This System"
Echo "msg" "-U --uninstall : Uninstall $rsname from This System"
echo
}

#下载子函数
Download_src() {
[ -s "${src_url##*/}" ] && Echo "msg" "[${src_url##*/}] found" { wget --tries=6 -c --no-check-certificate $src_url; sleep 1; } #判断下载是否已存在
if [ ! -e "${src_url##*/}" ]; then
Echo "msg" "${src_url##*/} download failed, Please check!"
kill -9 $$
fi
}

#调用方法
#src_url=资源URL地址 && Download_src

Install()
{
pushd ${base_dir}/src
Echo "success" "安装完成"
popd
}

Uninstall()
{

Echo "success" "卸载完成"
}

Update()
{
Echo "msg" "暂未开通"
}

#主函数,脚本入口
Main()
{
case $flag in
'-h' '--help' '?' )
showhelp
exit
;;
'--install' '-i' )
Install
exit
;;
'--uninstall' '-U' )
Uninstall
exit
;;
'--update' '-u' )
Update
exit
;;
* )
showhelp
exit
;;
esac
}

Main