-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbash_script_template
executable file
·185 lines (153 loc) · 3.24 KB
/
bash_script_template
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
#!/usr/bin/env bash
normal=$(tput sgr0)
bold=$(tput bold)
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
# Usage: info "message"
# Example: info "It's Working"
# Output (to STDOUT): [...] It's Working
info ()
{
printf "%b" "[${green}...${normal}] $1\n"
}
# Usage: prompt "question"
# Example: prompt "What's Your Name?"
# Output (to STDOUT): [ ? ] What's Your Name?
prompt ()
{
printf "%b" "[${yellow} ? ${normal}] $1 "
}
# Usage: error ["message"]
# Example: error "You're Ugly"
# Output (to STDERR): [ERROR] /script/name: You're Ugly
error ()
{
printf "%b" "[${red}ERROR${normal}] ${0}: ${1:-'Unkown Error'}\n" >&2
}
# Usage: fail "message" [exit_code]
# Example: fail "Unknown Option" 254
fail ()
{
error "$1"
case $2 in
''|*[!0-9]*)
exit_code=1
;;
*)
exit_code=$2
;;
esac
exit $exit_code
}
# ask function https://gist.github.com/davejamesmiller/1965569
# Usage: ask "Yes or No?" [default option]
# Example: if "Yes or No?" Y; then
ask ()
{
while true; do
if [ "${2:-}" = "Y" ]; then
prompt_tail="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt_tail="y/N"
default=N
else
prompt_tail="y/n"
default=
fi
# Ask the question
prompt "$1 [$prompt_tail]"
read REPLY
# Default?
if [ -z "$REPLY" ]; then
REPLY=$default
fi
# Check if the reply is valid
case "$REPLY" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
# Usage: show_help [exit_code]
# Example: show_help 254
show_help ()
{
cat <<HELP
Usage:
$0 [OPTION]...
OPTIONS:
--help, -h: Show this help and exit
Example:
$0 **example**
HELP
# Fail with general error if status code passed
case $1 in
''|*[!0-9]*)
exit_code=1
;;
*)
exit_code=$1
;;
esac
exit $exit_code
}
argument_expected()
{
fail "$1 expected an argument"
}
finally()
{
if [ ! -z $example_one ]; then
info "Finally, Example One: \"$example_one\""
fi
if [ ! -z $example_two ]; then
info "Finally, Example Two: \"$example_two\""
fi
}
check_args()
{
while [ ! -z "$1" ]; do
local arg="$1"
case "$1" in
-h|--help)
local help=0
shift
;;
-ex1|--example-one)
shift
if [ -z $1 ] || [[ "$1" == -* ]]; then
argument_expected $arg
fi
example_one=$1
shift
;;
-ex2|--example-two)
# Simple boolean flag–call function here
example_two="Two"
shift
;;
*)
fail "Unknown option $1"
shift
;;
esac
done
if [ ! -z $help ]; then
show_help $help
fi
finally
exit 0
}
main()
{
if [ -z $1 ]; then
show_help
fi
check_args "$@"
}
main "$@"