-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
functions.bash
279 lines (247 loc) · 7.01 KB
/
functions.bash
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env bash
# __ _ _
# / _|_ _ _ __ ___| |_(_) ___ _ __ ___
# | |_| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
# | _| |_| | | | | (__| |_| | (_) | | | \__ \
# |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
#
# Shell function examples and boilerplate.
#
# The functions here are intended to be included in the interactive shell,
# which is done by defining them in a shell init file like `~/.bashrc`.
#
# Bash Boilerplate: https://github.com/xwmx/bash-boilerplate
#
# Copyright (c) 2016 William Melody • hi@williammelody.com
###############################################################################
# Simple shell function with help / usage.
#
# This function provides an example of a simple shell function with help /
# usage information that is displayed with either the `-h` or `--help` flag.
###############################################################################
one() {
if [[ "${1:-}" =~ ^-h$|^--help$ ]]
then
cat <<HEREDOC
Usage:
one
one -h | --help
Options:
-h --help Display this usage information.
Description:
Say 'hello'.
HEREDOC
return 0
fi
printf "Hello.\\n"
}
###############################################################################
# Simple shell function with help / usage and option flags.
#
# This function provides an example of a simple shell function with help /
# usage information that is displayed with either the `-h` or `--help` flag.
# This example also demonstrates how to include an option flag to provide
# optional behavior. Only one option can be used at a time and it is expected
# to be in the first position.
###############################################################################
two() {
if [[ "${1:-}" =~ ^-h$|^--help$ ]]
then
cat <<HEREDOC
Usage:
two
two --all
two -h | --help
Options:
--all Say 'hello' to everyone.
-h --help Display this usage information.
Description:
Say 'hello'.
HEREDOC
elif [[ "${1:-}" == "--all" ]]
then
printf "Hello, everyone!\\n"
else
printf "Hello!\\n"
fi
}
###############################################################################
# Shell function with help / usage and option flags and option parsing.
#
# This function provides an example of a simple shell function with help /
# usage information that is displayed with either the `-h` or `--help` flag.
# This example also shows how to do basic option parsing in a function.
###############################################################################
three() {
local _all=0
local _arguments=()
local _help=0
for __arg in "${@:-}"
do
case "${__arg}" in
--all)
_all=1
;;
-h|--help)
_help=1
;;
*)
_arguments+=("${__arg}")
;;
esac
done
if ((_help))
then
cat <<HEREDOC
Usage:
three
three --all
three -h | --help
Options:
--all Say 'hello' to everyone.
-h --help Display this usage information.
Description:
Say 'hello'.
HEREDOC
elif ((_all))
then
printf "Hello, everyone!\\n"
else
printf "Hello!\\n"
fi
}
###############################################################################
# Shell function with parsing for options with values.
#
# This function provides an example of a simple shell function with help /
# usage information that is displayed with either the `-h` or `--help` flag.
# This example also shows how to do option parsing with values in a function.
###############################################################################
four() {
# Usage: __get_option_value <option> <value>
__get_option_value() {
local __arg="${1:-}"
local __val="${2:-}"
if [[ -n "${__val:-}" ]] && [[ ! "${__val:-}" =~ ^- ]]
then
printf "%s\\n" "${__val}"
else
printf "%s %s requires a valid argument.\\n" \
"$(tput setaf 1)!$(tput sgr0)" \
"${__arg}"
return 1
fi
}
local _all=0
local _arguments=()
local _help=0
local _to=
while ((${#}))
do
local __arg="${1:-}"
local __val="${2:-}"
case "${__arg}" in
-a|--all)
_all=1
;;
-h|--help)
_help=1
;;
-t|--to)
if ! _to="$(__get_option_value "${__arg}" "${__val:-}")"
then
printf "%s\\n" "${_to}"
return 1
fi
shift
;;
*)
_arguments+=("${__arg}")
;;
esac
shift
done
if ((_help))
then
cat <<HEREDOC
Usage:
four
four --all
four -h | --help
four (-t | --to) <name>
Options:
--all Say 'hello' to everyone.
-h --help Display this usage information.
-t <name> --to <name> Say 'hello' to <name>.
Description:
Say 'hello'.
HEREDOC
elif ((_all))
then
printf "Hello, everyone!\\n"
elif [[ -n "${_to:-}" ]]
then
printf "Hello, %s!\\n" "${_to:-}"
else
printf "Hello!\\n"
fi
}
###############################################################################
# Simple wrapper with help / usage and option flags.
#
# This wrapper function provides an example of a simple wrapper for an
# existing command, adding help / usage information that is displayed with
# either the `-h` or `--help` flag. This example also demonstrates how to
# include an option flag to provide optional behavior. Only one option can be
# used at a time and it is expected to be in the first position.
#
# Wrapper functions are useful for adding functionality and options to an
# existing command, or even simply adding usage information to a command that
# doesn't otherwise provide it.
###############################################################################
# Save the existing `yes` exectuable path to a variable so it can be used
# after the name `yes` is redefined.
_YES_COMMAND="$(which yes)"
yes() {
if [[ "${1:-}" =~ ^-h$|^--help$ ]]
then
cat <<HEREDOC
Usage:
yes [<expletive>]
yes --quiet
yes -h | --help
Options:
--quiet Suppress output.
-h --help Display this usage information.
Description:
A wrapper for \`yes\`, which outputs <expletive> or, by default, 'y' forever.
For more information, run \`man yes\`.
HEREDOC
elif [[ "${1:-}" == "--quiet" ]]
then
"${_YES_COMMAND}" "${@}" 1> /dev/null
else
"${_YES_COMMAND}" "${@}"
fi
}
###############################################################################
# Anonymous Function / Immediately-invoked function expression (IIFE)
#
# A simple anonymous throway function pattern that leverages common conventions
# from JavaScript and other languages. An underscore (`_`) function name
# indicates an anonymous function, which is then immediately invoked after
# the definition.
#
# Anonymous functions can be useful for shell initialization functions that are
# intended to be executed only once.
#
# Source (credit to https://stackoverflow.com/users/10559/jwfearn):
# https://stackoverflow.com/a/24538676
#
# More Information:
# https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
###############################################################################
_() {
printf "Hello World.\\n"
}; _ "$@"
unset -f _