-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
template-v1.sh
executable file
·133 lines (102 loc) · 2.5 KB
/
template-v1.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
#!/usr/bin/env bash
#: Your comments here.
set -o errexit
set -o nounset
set -o pipefail
work_dir=$(dirname "$(readlink --canonicalize-existing "${0}" 2> /dev/null)")
readonly conf_file="${work_dir}/script.conf"
readonly error_reading_conf_file=80
readonly error_parsing_options=81
readonly script_name="${0##*/}"
a_option_flag=0
abc_option_flag=0
flag_option_flag=0
trap clean_up ERR EXIT SIGINT SIGTERM
usage() {
cat <<USAGE_TEXT
Usage: ${script_name} [-h | --help] [-a <ARG>] [--abc <ARG>] [-f | --flag]
DESCRIPTION
Your description here.
OPTIONS:
-h, --help
Print this help and exit.
-f, --flag
Description for flag option.
-a
Description for the -a option.
--abc
Description for the --abc option.
USAGE_TEXT
}
clean_up() {
trap - ERR EXIT SIGINT SIGTERM
# Remove temporary files/directories, log files or rollback changes.
}
die() {
local -r msg="${1}"
local -r code="${2:-90}"
echo "${msg}" >&2
exit "${code}"
}
if [[ ! -f "${conf_file}" ]]; then
die "error reading configuration file: ${conf_file}" "${error_reading_conf_file}"
fi
# shellcheck source=script.conf
. "${conf_file}"
parse_user_options() {
local -r args=("${@}")
local opts
# The following code works perfectly for
opts=$(getopt --options a:,f,h --long abc:,help,flag -- "${args[@]}" 2> /dev/null) || {
usage
die "error: parsing options" "${error_parsing_options}"
}
eval set -- "${opts}"
while true; do
case "${1}" in
--abc)
abc_option_flag=1
readonly abc_arg="${2}"
shift
shift
;;
-a)
a_option_flag=1
readonly a_arg="${2}"
shift
shift
;;
--help|-h)
usage
exit 0
shift
;;
--flag|-f)
flag_option_flag=1
shift
;;
--)
shift
break
;;
*)
break
;;
esac
done
}
parse_user_options "${@}"
if ((flag_option_flag)); then
echo "flag option set"
fi
if ((abc_option_flag)); then # Check if the flag options are set or ON:
# Logic for when --abc is set.
# "${abc_arg}" should also be set.
echo "Using --abc option -> arg: [${abc_arg}]"
fi
if ((a_option_flag)); then
# Logic for when -a is set.
# "${a_arg}" should also be set.
echo "Using -a option -> arg: [${a_arg}]"
fi
exit 0