-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBashInit
executable file
·156 lines (136 loc) · 4.88 KB
/
BashInit
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
#!/bin/bash
#: Title : xxx.bash
#: Usage : xxx.bash [-m] [-i item] target
#: Synopsis : for bash script setup
#: Date : 2015-02-13 11:23:04
#: Author : shulei
#: version : 1.0
#: Description : be used to setup bash script for jenkins job or daily bash script
#: Required : target => which target
#: Options : -i set items
#: : -m set log
#: : -h show usage
#########################################################################################
# ---------------------- import lib -------------------------------------------------#
#########################################################################################
import_bsfl() {
local __ORIGIN_PATH__="$PWD"
script_path="${0%/*}" # remove the script name ,get the path
script_path=${script_path/\./$(pwd)} # if path start with . , replace with $PWD
cd "${script_path}"
# -------------------- start get bsfl --------------------
if ! [ -f "bsfl" ]; then
# down from local
wget -q http://192.168.65.56/qinsl0106/bsfl/raw/master/bsfl
fi
if ! [ -f "bsfl" ]; then
# down from gist
wget -q https://gist.githubusercontent.com/qinshulei/af2dfb4aec520ab45771/raw/696a9a03540d423285e7658d6b1a7bb63b87fbbb/bsfl
fi
if ! [ -f "bsfl" ]; then
# down from gist
echo "can't find bsfl!"
exit 1
fi
# -------------------- end get bsfl --------------------
# import bsfl lib
source ./bsfl
cd "$__ORIGIN_PATH__"
}
import_bsfl
#########################################################################################
# ---------------------- main function -------------------------------------------------#
#########################################################################################
main() {
cd_to_script_path
init_params "$@"
print_params
sleep_1_minute_for_check_params
## start process
do_process
}
#####################################################################################
#-------------------------------logic function--------------------------------------#
#####################################################################################
show_usage() {
cat <<- _EOF_
A init template for bash script
Examples:
$ xxx.bash [-m] [-i item] target
_EOF_
exit 1
}
init_params() {
## init parameters
## constant and deault value
MESSAGE_ENABLED=no
items=()
target=
init_value_from_env
init_value_from_opts "$@"
## check the parameters
if is_null_or_empty target;then
if is_null_or_empty TARGET;then
show_usage
die 1 "must input the target!"
else
#use env target
target="${TARGET}"
fi
fi
}
init_value_from_env() {
## base on enviroment , set default items value .
is_true ITEM_ENABLED && items[${#items[*]}]=Item
}
init_value_from_opts() {
## parse command-line options
while getopts "hmi:" var
do
case $var in
m) MESSAGE_ENABLED=y
;;
h) show_usage
;;
i)
if contains ${items[*]} "${OPTARG}";then
option_enabled MESSAGE_ENABLED && printf "env has contains this items : %s \n" "${OPTARG}"
else
items[${#items[*]}]=${OPTARG}
fi
;;
esac
done
shift $(( OPTIND - 1 ))
target=${1}
}
print_params(){
option_enabled MESSAGE_ENABLED && printf "selected items : %s\n" "${items[*]}"
option_enabled MESSAGE_ENABLED && printf "target : %s\n" "${target}"
}
do_process(){
if contains ${items[*]} "item";then
option_enabled MESSAGE_ENABLED && printf "========== start process : %s\n" "item"
# do something
fi
}
#####################################################################################
#----------------------------common function ---------------------------------------#
#####################################################################################
sleep_1_minute_for_check_params() {
option_enabled MESSAGE_ENABLED && echo "============================================================"
option_enabled MESSAGE_ENABLED && echo "sleep 1 minute, for check params again"
sleep 1m
option_enabled MESSAGE_ENABLED && echo "sleep end, start process"
option_enabled MESSAGE_ENABLED && echo "============================================================"
}
cd_to_script_path() {
## Script path
script_path="${0%/*}" # remove the script name ,get the path
script_path=${script_path/\./$(pwd)} # if path start with . , replace with $PWD
cd "${script_path}"
}
### ------------------------ start -----------------------------
__ORIGIN_PATH__="$PWD"
main "$@"
cd "$__ORIGIN_PATH__"