Skip to content

Instantly share code, notes, and snippets.

@qinshulei
Last active August 29, 2015 14:05
Show Gist options
  • Save qinshulei/bb6fa9f2be4471d3571f to your computer and use it in GitHub Desktop.
Save qinshulei/bb6fa9f2be4471d3571f to your computer and use it in GitHub Desktop.
bash-setup
#!/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"
for i in $(seq 6);do
sleep 10s
done
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__"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment