-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibis
111 lines (103 loc) · 2.33 KB
/
libis
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
#!/usr/bin/env bash
# Determine if a given environment variable exists and if it is an array.
# This is true for normal arrays and for associative arrays.
#
# $1 - Name of environment variable.
#
# Examples
#
# var=(abc)
#
# if is::array var; then
# echo "This is an array"
# echo "Be careful to use the variable's name in the function call."
# fi
#
# declare -A associativeArray
# associativeArray[test]=value
#
# if is::array associativeArray; then
# echo "This is also an array"
# fi
#
# right=wrong
# wrong=()
#
# if is::array $right; then
# echo "This also says it is an array, which is incorrect."
# echo 'The correct call is `is::array right` without the $.'
# fi
#
# Returns true (0) if the named variable exists and if it is an array.
# Returns false (1) otherwise.
is::array() {
[[ "$(declare -p "$1" 2> /dev/null)" == "declare -"[aA]* ]]
}
# Determine if a given environment variable exists and if it is an associative
# array.
#
# $1 - Name of environment variable.
#
# Examples
#
# declare -A associativeArray
# associativeArray[test]=value
#
# if is::associativeArray associativeArray; then
# echo "This is also an array"
# fi
#
# var=(abc)
#
# if is::associativeArray var; then
# echo "This is false - $var is not an associative array"
# fi
#
# Returns true (0) if the named variable exists and if it is an associative
# array. Returns false (1) otherwise.
is::associativeArray() {
[[ "$(declare -p "$1" 2> /dev/null)" == "declare -A"* ]]
}
# Determine if the given name is a defined function.
#
# $1 - Function name to check.
#
# Examples
#
# moo () {
# echo "This is a function"
# }
#
# if is::function moo; then
# echo "moo is a defined function"
# fi
#
# Returns true (0) if the name is a function, false (1) otherwise.
is::function() {
if declare -Ff "$1" > /dev/null; then
return 0
fi
return 1
}
# Determine if a variable is assigned, even if it is assigned an empty value.
#
# $1 - Variable name to check.
#
# Examples
#
# unset abc
#
# if ! is::set abc; then
# echo "The variable is not set"
# fi
#
# abc=""
#
# if is::set abc; then
# echo "This is true, the variable is set"
# fi
#
# Returns true (0) if the variable is set, false (1) if the variable is unset.
is::set() {
[[ "${!1-a}" == "${!1-b}" ]]
}