-
Notifications
You must be signed in to change notification settings - Fork 10
/
functions.sh
113 lines (104 loc) · 3.6 KB
/
functions.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
# --------------------------------------------------------------------------------------
# check environment
# --------------------------------------------------------------------------------------
function check_env() {
if [[ -x $(which lsb_release 2>/dev/null) ]]; then
CODENAME=$(lsb_release -c -s)
if [[ $CODENAME != "precise" && $CODENAME != "quantal" ]]; then
echo "This code was tested on precise and quantal only."
exit 1
fi
else
echo "You can run this code on Ubuntu OS only."
exit 1
fi
export CODENAME
}
# --------------------------------------------------------------------------------------
# check os vendor
# --------------------------------------------------------------------------------------
function check_os() {
VENDOR=$(lsb_release -i -s)
export VENDER
}
function check_codename() {
VENDOR=$(lsb_release -c -s)
export CODENAME
}
# --------------------------------------------------------------------------------------
# get field function
# --------------------------------------------------------------------------------------
function get_field() {
while read data; do
if [ "$1" -lt 0 ]; then
field="(\$(NF$1))"
else
field="\$$(($1 + 1))"
fi
echo "$data" | awk -F'[ \t]*\\|[ \t]*' "{print $field}"
done
}
# --------------------------------------------------------------------------------------
# package installation function
# --------------------------------------------------------------------------------------
function install_package() {
apt-get -y install "$@"
}
# --------------------------------------------------------------------------------------
# restart function
# --------------------------------------------------------------------------------------
function restart_service() {
check_os
if [[ "$VENDOR" = "Ubuntu" ]]; then
sudo /usr/bin/service $1 restart
elif [[ "$VENDOR" = "Debian" ]]; then
sudo /usr/sbin/service $1 restart
else
echo "We does not support your distribution."
exit 1
fi
}
# --------------------------------------------------------------------------------------
# restart function
# --------------------------------------------------------------------------------------
function start_service() {
check_os
if [[ "$VENDOR" = "Ubuntu" ]]; then
sudo /usr/bin/service $1 start
elif [[ "$VENDOR" = "Debian" ]]; then
sudo /usr/sbin/service $1 start
else
echo "We does not support your distribution."
exit 1
fi
}
# --------------------------------------------------------------------------------------
# stop function
# --------------------------------------------------------------------------------------
function stop_service() {
check_os
if [[ "$VENDOR" = "Ubuntu" ]]; then
sudo /usr/bin/service $1 stop
elif [[ "$VENDOR" = "Debian" ]]; then
sudo /usr/sbin/service $1 stop
else
echo "We does not support your distribution."
exit 1
fi
}
# --------------------------------------------------------------------------------------
# print syntax
# --------------------------------------------------------------------------------------
function print_syntax() {
cat ./usage
exit 1
}
# --------------------------------------------------------------------------------------
# check parameter
# --------------------------------------------------------------------------------------
function check_para() {
if [ ! "$1" ]; then
echo "This paramter $1 is not available."
exit 1
fi
}