-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.sh
executable file
·89 lines (71 loc) · 1.54 KB
/
util.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
#!/bin/bash
# using : contains util functions
# author : [email protected]
# variables
log_file=log/process.log
# create directory to store log file
mkdir -vp log
# using : current time by format
# return : date time string format by %Y-%m-%d %H:%M:%S
current_time() {
echo $(date +"%Y-%m-%d %H:%M:%S")
}
# write information into file and console
# log file in ./log/process.log
# prams:
# $1: message
# $2: result in enum { true, false }. true is successfull, false is error
# return: 0 on successfull, 1 on error
log() {
# get time
log_time=$(current_time)
# convert result
result="?"
if [[ $2 == 0 ]]; then
result="ok"
else
result="no"
fi
# log to file
printf "%s\n" "$log_time" >> "$log_build_file"
printf "%-78s%2s\n\n" "$1" "$result" >> "$log_build_file"
# log to console
printf "%s\n" "$log_time"
printf "%-78s%2s\n\n" "$1" "$result"
# exit if error
if [[ $2 != 0 ]]; then
exit 1
fi
# successfull
return 0
}
# clear log file
# log file in ./log/process.log
# return: 0 on successfull, 1 on error
clear_log() {
if [ -f $log_file ]; then
> $log_file
fi
# check error
if [[ $? != 0 ]]; then
return 1
fi
# successfull
return 0
}
# exit process when error
# params:
# $1: exit code
exit_on_error() {
if [[ $? != 0 ]]; then
exit 1
fi
}
# escape string
# params
# $1: string to escape
# stdout
# string escaped
escape_str() {
echo $1 | sed -e 's/[]\/$*.^|[]/\\&/g'
}