forked from FluxionNetwork/fluxion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayUtils.sh
executable file
·30 lines (25 loc) · 1.02 KB
/
ArrayUtils.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
#!/usr/bin/env bash
if [ "$ArrayUtilsVersion" ]; then return 0; fi
readonly ArrayUtilsVersion="1.0"
# Due to the fact we're passing arrays via indirection,
# we've got to mangle variable names used within array
# functions to prevent accidentally having a naming
# conflic with an array, for example, an array with the
# "choice" identifier in the input_choice function.
# Eventually, input_choice's "choice" variable will
# be indirectly expanded rather than the choice array.
function array_contains() {
local __array_contains__item
# An efficient way to pass arrays around in bash
# is to perform indirect expansion by using the
# expansion symbol, $, along with the indirection
# symbol, !, in curly braces, ${! }, resulting in:
# function call: array_contains array[@] "text"
# funct params: $1 = "array[@]" $2 = "text"
# indirect exp: ${!1} => ${array[@]} (replaced!)
for __array_contains__item in "${!1}"; do
[[ "$__array_contains__item" == "$2" ]] && return 0
done
return 1 # Not found
}
# FLUXSCRIPT END