-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutils.sh
366 lines (303 loc) · 7.48 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
#!/usr/bin/env bash
# https://github.com/wklken/bash-utils
#====================== echo ======================
echo_step() {
# Usage: echo_step "1. this is the step 1"
echo -e '\033[0;32m'"$1"'\033[0m'
}
echo_separator() {
# Usage: echo_separator
echo "===================================================="
}
echo_in_processing_bar() {
# Usage: bar 1 10
# ^----- Elapsed Percentage (0-100).
# ^-- Total length in chars.
((elapsed=$1*$2/100))
# Create the bar with spaces.
printf -v prog "%${elapsed}s"
printf -v total "%$(($2-elapsed))s"
printf '%s\r' "[${prog// /-}${total}]"
}
#====================== log ======================
log_info() {
# Usage: log_info "this is the info log message"
NOW=$(date +"%Y-%m-%d %H:%M:%S")
echo "${NOW} [INFO] $1"
}
log_warnning() {
# Usage: log_warnning "this is the warning log message"
NOW=$(date +"%Y-%m-%d %H:%M:%S")
echo "${NOW} [WARNNING] $1"
}
log_error() {
# Usage: log_error "this is the error log message"
NOW=$(date +"%Y-%m-%d %H:%M:%S")
echo "${NOW} [ERROR] $1"
}
log_exit() {
# Usage: log_exit "the log message before exit"
log_error "$1"
exit 1
}
#====================== action ======================
do_exit_0() {
# Usage: do_exit_0 "the log message before exit 0"
if [ ! -z "$1" ]
then
log_info "$1"
fi
exit 0
}
do_exit_1() {
# Usage: do_exit_0 "the log message before exit 1"
if [ ! -z "$1" ]
then
log_info "$1"
fi
exit 1
}
#====================== confirm======================
confirm() {
# Usage: x=$(confirm "do you want to continue?")
# if [ "$x" = "yes" ]
QUESTION="$1"
read -p "${QUESTION} [yN] " ANSWER
if [[ "${ANSWER}" == "y" ]] || [[ "${ANSWER}" == "Y" ]]
then
echo "yes"
else
echo "no"
fi
}
#====================== if ======================
#====================== if-then ======================
# exit
if_error_then_exit() {
# Usage: if_error_then_exit $? "fail, and exit"
if [ "$1" -ne 0 ]
then
log_error "$2"
exit 1
fi
}
if_empty_then_exit() {
# Usage: if_empty_then_exit ${1} "param 1 required"
if [ -z "$1" ]
then
log_error "$2"
exit 1
fi
}
if_empty_then_return_default() {
# Usage: A=$(if_empty_return_default "${1}" 123)
if [ -z "${1}" ]
then
echo "${2}"
else
echo "${1}"
fi
}
if_empty_then_log_warnning() {
# Usage: if_empty_then_log_warnning "$1" "the param 1 is empty"
if [ -z "${1}" ]
then
log_warnning "${2}"
fi
}
if_path_not_exist_then_exit() {
# Usage: if_path_not_exist_then_exit "/tmp/a.txt" "/tmp/a.txt is not exists"
if [ ! -e "$1" ]
then
log_error "$2"
exit 1
fi
}
# action
if_file_not_exist_then_touch() {
# Usage: if_file_not_exist_then_touch "/tmp/a.txt"
[ -e "$1" ] || touch "$1"
return $?
}
if_dir_not_exist_then_mkdir() {
# Usage: if_dir_not_exist_then_mkdir "/tmp/abc"
[ -d "$1" ] || mkdir -p "$1"
return $?
}
if_file_exist_then_remove() {
# Usage: if_file_exist_then_remove "/tmp/a.txt"
if [ -e "$1" ]
then
rm "$1"
return $?
fi
}
if_dir_exist_then_remove() {
# Usage: if_dir_exist_then_remove "/tmp/abc"
if [ -e "$1" ]
then
rm -rf "$1"
return $?
fi
}
function if_file_or_dir_exist_then_move_to() {
if [ -e "$1" ]
then
mv "$1" "$2"
return $?
fi
}
function if_file_or_dir_exist_then_copy_to() {
if [ -e "$1" ]
then
cp -r "$1" "$2"
return $?
fi
}
#====================== is ======================
is_command_exists () {
type "$1" &> /dev/null ;
}
#====================== dir ======================
remkdir() {
# Usage: remkdir "/tmp/abc"
if [ -e "$1" ]
then
rm -rf "$1"
local rc=$?
if [ "$rc" -ne 0 ]
then
log_error "remkdir: fail, when do [rm -rf ${1}]"
return "$rc"
fi
fi
mkdir -p "$1"
return $?
}
#====================== date ======================
get_today() {
# Usage: x=$(get_today)
echo "$(date +%Y%m%d)"
}
get_day_before_from_now() {
# Usage: x=$(get_day_before_from_now 2)
COUNT="$1"
TODAY=$(date +%Y%m%d)
echo $(date -d "$TODAY ${COUNT} days ago" +%Y%m%d)
}
get_day_before_from() {
# Usage: y=$(get_day_before_from 20170905 2)
FROM="$1"
COUNT="$2"
echo $(date -d "${FROM} ${COUNT} days ago" +%Y%m%d)
}
generate_date_list () {
# Usage: generate_date_list 20120304 20120405
TMP=$(date -d "$1" "+%Y%m%d")
TO=$(date -d "$2" "+%Y%m%d")
while [ "$(date -d $TO '+%s')" -ge "$(date -d $TMP '+%s')" ]; do
echo $TMP
TMP=$(date -d "$TMP 1day" "+%Y%m%d")
done
}
#====================== tar ======================
do_tar() {
# Usage: do_tar example.tar.gz example
PKG_NAME="${1}"
DIR="${2}"
if_file_exist_then_remove "${PKG_NAME}"
tar -czf "${PKG_NAME}" "${DIR}"
if_error_then_exit "$?" "tar -czf ${PKG_NAME} ${DIR} fail"
}
#====================== string ======================
# reference https://github.com/dylanaraps/pure-bash-bible
string_trim() {
# Usage: string_trim " example string "
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
string_split() {
# Usage: string_split "string" "delimiter"
IFS=$'\n' read -d "" -ra arr <<< "${1//$2/$'\n'}"
printf '%s\n' "${arr[@]}"
}
string_lstrip() {
# Usage: string_lstrip "string" "pattern"
printf '%s\n' "${1##$2}"
}
string_rstrip() {
# Usage: string_rstrip "string" "pattern"
printf '%s\n' "${1%%$2}"
}
# Requires bash 4+
string_to_lower() {
# Usage: string_to_lower "string"
printf '%s\n' "${1,,}"
}
# Requires bash 4+
string_to_upper() {
# Usage: string_to_upper "string"
printf '%s\n' "${1^^}"
}
string_contains() {
# Usage: string_contains hello he
[[ "${1}" == *${2}* ]]
}
string_starts_with() {
# Usage: string_starts_with hello he
[[ "${1}" == ${2}* ]]
}
string_ends_with() {
# Usage: string_ends_wit hello lo
[[ "${1}" == *${2} ]]
}
string_regex() {
# Usage: string_regex "string" "regex"
[[ $1 =~ $2 ]] && printf '%s\n' "${BASH_REMATCH[1]}"
}
#====================== array ======================
# reference https://github.com/dylanaraps/pure-bash-bible
array_reverse() {
# Usage: array_reverse "array"
shopt -s extdebug
f()(printf '%s\n' "${BASH_ARGV[@]}"); f "$@"
shopt -u extdebug
}
array_remove_dups() {
# Usage: array_remove_dups "array"
declare -A tmp_array
for i in "$@"; do
[[ "$i" ]] && IFS=" " tmp_array["${i:- }"]=1
done
printf '%s\n' "${!tmp_array[@]}"
}
array_random_element() {
# Usage: array_random_element "array"
local arr=("$@")
printf '%s\n' "${arr[RANDOM % $#]}"
}
#====================== program ======================
run_command_in_background() {
# Usage: run_command_in_background ./some_script.sh
(nohup "$@" &>/dev/null &)
}
#====================== others ======================
generate_uuid() {
# Usage: generate_uuid
C="89ab"
for ((N=0;N<16;++N)); do
B="$((RANDOM%256))"
case "$N" in
6) printf '4%x' "$((B%16))" ;;
8) printf '%c%x' "${C:$RANDOM%${#C}:1}" "$((B%16))" ;;
3|5|7|9)
printf '%02x-' "$B"
;;
*)
printf '%02x' "$B"
;;
esac
done
printf '\n'
}