-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.sh
412 lines (359 loc) · 7.72 KB
/
utils.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/bin/bash
#
# utils.sh
#
# Copyright (C) 2018-2023 Eric MA <[email protected]>. All Rights Reserved.
#
# History:
# 2019/06/28 - [Eric MA] Created file
#
# Maintainer: you <[email protected]>
# Created: 2019-06-28
# LastChange: 2019-12-31
# Version: v0.0.04
#
logo_path=./utils/.logo
logo_files=($(ls $logo_path | awk '{print $1}'))
show_logo()
{
cur_second=`date +%S`
logo_num=${#logo_files[@]}
index=$((10$cur_second%$logo_num))
#echo "logo_num:$logo_num"
cat $logo_path/${logo_files[index]}
}
opt_verbose=1
__echo()
{
opt="$1"
shift
_msg="$@"
if [ "$opt_no_color" = 1 ] ; then
# strip ANSI color codes
_msg=$(/bin/echo -e "$_msg" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")
fi
# explicitly call /bin/echo to avoid shell builtins that might not take options
/bin/echo $opt -e "$_msg"
}
_echo()
{
if [ $opt_verbose -ge $1 ]; then
shift
__echo '' "$@"
fi
}
_echo_nol()
{
if [ $opt_verbose -ge $1 ]; then
shift
__echo -n "$@"
fi
}
_info()
{
_echo 1 "$@"
}
_info_nol()
{
_echo_nol 1 "$@"
}
# print status function
pstatus()
{
if [ "$opt_no_color" = 1 ]; then
_info_nol "$2"
else
case "$1" in
red) col="\033[101m\033[30m";;
green) col="\033[102m\033[30m";;
yellow) col="\033[103m\033[30m";;
blue) col="\033[104m\033[30m";;
*) col="";;
esac
_info_nol "$col $2 \033[0m"
fi
[ -n "$3" ] && _info_nol " ($3)"
_info
}
# show_header common code
VERSION=0.1
tool_name="tool"
show_header()
{
_info "\033[1;34m${tool_name} v$VERSION\033[0m"
_info
}
# show_header common code
usage=(
)
options=(
)
ret_codes=(
)
examples=(
)
show_usage()
{
cat <<EOF
Usage:
${usage[0]}
Options:
--no-color Don't use color codes
--verbose, -v Increase verbosity level
${options[0]}
Return codes:
${ret_codes[0]}
Examples:
`basename $0` --version
`basename $0` --help
`basename $0` --disclaimer
${examples[0]}
IMPORTANT:
A false sense of security is worse than no security at all.
Please use the --disclaimer option to understand exactly what this script does.
EOF
}
parse_opt_file()
{
# parse_opt_file option_name option_value
option_name="$1"
option_value="$2"
if [ -z "$option_value" ]; then
show_header
show_usage
echo "$0: error: --$option_name expects one parameter (a file)" >&2
exit 1
elif [ ! -e "$option_value" ]; then
show_header
echo "$0: error: couldn't find file $option_value" >&2
exit 1
elif [ ! -f "$option_value" ]; then
show_header
echo "$0: error: $option_value is not a file" >&2
exit 1
elif [ ! -r "$option_value" ]; then
show_header
echo "$0: error: couldn't read $option_value (are you root?)" >&2
exit 1
fi
echo "$option_value"
exit 0
}
parse_opt_dir()
{
# parse_opt_dir option_name option_value
option_name="$1"
option_value="$2"
if [ -z "$option_value" ]; then
show_header
show_usage
echo "$0: error: --$option_name expects one parameter (a directory)" >&2
exit 1
elif [ ! -d "$option_value" ]; then
show_header
echo "$0: error: couldn't find directory $option_value" >&2
echo "$0: error: --$option_name expects one parameter (a directory)" >&2
exit 1
fi
echo "$option_value"
exit 0
}
#**********
# my utils
#**********
#set color
set_color()
{
color_failed="\e[0;31m"
color_success="\e[0;32m"
color_yellow='\E[1;33m'
color_blue='\E[1;34m'
color_reset="\e[00m"
}
#获取开始时间和路径
get_start_time()
{
start_time=$(date +"%s")
}
#echo install time
echo_execu_time()
{
end_time=$(date +"%s")
tdiff=$(($end_time-$start_time))
hours=$(($tdiff / 3600 ))
mins=$((($tdiff % 3600) / 60))
secs=$(($tdiff % 60))
echo
echo -n -e "${color_success}#### executed completed successfully! "
if [ $hours -gt 0 ] ; then
echo -n -e "($hours:$mins:$secs (hh:mm:ss))"
elif [ $mins -gt 0 ] ; then
echo -n -e "($mins:$secs (mm:ss))"
elif [ $secs -gt 0 ] ; then
echo -n -e "($secs seconds)"
fi
echo -e " ####${color_reset}"
echo
}
# log APIs
error_exit()
{
echo -e "${color_failed}Error: $1${color_reset}" 1>&2
exit 1
}
warning_log()
{
echo -e "${color_yellow}Warning: $1${color_reset}" 1>&2
}
blue_log()
{
echo -e "${color_blue}$1${color_reset}" 1>&2
}
successful_log()
{
echo -e "${color_success}$1${color_reset}"
}
# $1: progress: [0~100]
# $2: log
progress_log()
{
# $1 is must
if [[ -z $1 ]]; then
echo "${FUNCNAME[0]} at least need 1 param"
exit
fi
local now=$1
local all=100
local bar_log=$2
if [[ $now -gt $all ]]; then
now=$all
fi
local percent=`awk BEGIN'{printf "%f", ('$now'/'$all')}'`
local percent_len=`awk BEGIN'{printf "%d", (100*'$percent')}'`
printf "[%3d%%] %s\n" "$percent_len" "$bar_log"
}
#
check_chardev_file()
{
# check_file param_value
param_value="$1"
if [ -z "$param_value" ]; then
echo "$0: error[${FUNCNAME[1]}()]: expects one parameter (a dev node)" >&2
exit 1
elif [ ! -e "$param_value" ]; then
echo "$0: error[${FUNCNAME[1]}()]: couldn't find file $param_value" >&2
exit 1
elif [ ! -c ${param_value} ]; then
echo "$0: error[${FUNCNAME[1]}()]: $param_value is not a char file" >&2
exit 1
fi
echo "$param_value"
}
check_file()
{
# check_file param_value
param_value="$1"
if [ -z "$param_value" ]; then
echo "$0: error[${FUNCNAME[1]}()]: expects one parameter (a file)" >&2
exit 1
elif [ ! -e "$param_value" ]; then
echo "$0: error[${FUNCNAME[1]}()]: couldn't find file $param_value" >&2
exit 1
elif [ ! -f "$param_value" ]; then
echo "$0: error[${FUNCNAME[1]}()]: $param_value is not a file" >&2
exit 1
elif [ ! -r "$param_value" ]; then
echo "$0: error[${FUNCNAME[1]}()]: couldn't read $param_value (are you root?)" >&2
exit 1
fi
echo "$param_value"
}
check_dir()
{
# check_dir param_value
param_value="$1"
if [ -z "$param_value" ]; then
echo "$0: error[${FUNCNAME[1]}()]: expects one parameter (a directory)" >&2
exit 1
elif [ -L "$param_value" ]; then
echo "$0: error[${FUNCNAME[1]}()]: $param_value is a softlink" >&2
echo "$0: error[${FUNCNAME[1]}()]: expects one parameter (a directory)" >&2
exit 1
elif [ ! -d "$param_value" ]; then
echo "$0: error[${FUNCNAME[1]}()]: couldn't find directory $param_value" >&2
echo "$0: error[${FUNCNAME[1]}()]: expects one parameter (a directory)" >&2
exit 1
fi
echo "$param_value"
}
clean_file()
{
local file=$1
# clean_file file
if [ -f "$file" ]; then
rm $file
fi
}
date=`date +%F | sed 's/-//g'``date +%T | sed 's/://g'`
#获取开始时间和路径
function get_start_time()
{
start_time=$(date +"%s")
}
#set color
function set_color()
{
color_failed="\e[0;31m"
color_success="\e[0;32m"
color_reset="\e[00m"
}
#echo install time
function echo_install_time()
{
end_time=$(date +"%s")
tdiff=$(($end_time-$start_time))
hours=$(($tdiff / 3600 ))
mins=$((($tdiff % 3600) / 60))
secs=$(($tdiff % 60))
echo
echo -n -e "${color_success}#### update completed successfully! "
if [ $hours -gt 0 ] ; then
echo -n -e "($hours:$mins:$secs (hh:mm:ss))"
elif [ $mins -gt 0 ] ; then
echo -n -e "($mins:$secs (mm:ss))"
elif [ $secs -gt 0 ] ; then
echo -n -e "($secs seconds)"
fi
echo -e " ####${color_reset}"
echo
}
#检查root权限
function check_root_privileges()
{
if [ $UID -eq 0 ]; then
echo -e "${color_failed}>>> Error: Remove you root privileges!"
echo -e "Please input \"./`basename $0`\"${color_reset}"
exit
else
echo "You have normal privileges!"
fi
}
#shell脚本下载数据时,先检测网络的畅通性
function check_network()
{
#标识网络连接状态
online=1
#目标网站
target=www.baidu.com
local ret=`ping $target -c 3 | grep -q "ttl=" && echo "yes" || echo "no"`
if [[ $ret = "yes" ]]; then
#网络畅通
echo -e "====== ${FUNCNAME[0]}(): The Internet is connected ! ======"
online=1
else
#网络不畅通
echo
echo "the network connection is unavailable."
online=0
fi
}