This repository was archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc_functions.sh
More file actions
176 lines (153 loc) · 4.64 KB
/
misc_functions.sh
File metadata and controls
176 lines (153 loc) · 4.64 KB
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
#----------------------------------------------------------------------
# misc_functions.sh
#
# Miscelaneous functions
#----------------------------------------------------------------------
#----------------------------------------------------------------------
# mf_append <string> <string>
#
# Append to the front of an environment variable string. Appends with
# the ':' separator.
# ----------------------------------------------------------------------
mf_concat () {
if [ "$1" == "" ] || [ "$2" == "" ]; then # missing elements
echo "$1$2"
else
echo "$1:$2"
fi
}
#-----------------------------------------------------------------------------------
# mf_in_list <string> <list>
#
# Tests if a string is in a list (where the list is ":" separated) return 1 on false
# and 0 on true.
# ----------------------------------------------------------------------------------
mf_in_list (){
local string="$1"
local list="$2"
if [ "$list" == "" ] || [ "$string" == "" ]; then
return 1
fi
# split the list and search
local arr=$(echo "$list" | tr ":" "\n")
for x in $arr
do
[ "$x" == "$string" ] && return 0
done
return 1
}
mf_list_size (){
local list="$1"
if [ "$list" == "" ]; then
echo 1
else
# split the list and search
count=$(echo "$list" | tr ":" "\n" | wc -l)
echo $count
fi
}
# -----------------------------------------------------------------------------
# mf_cond_insert/append <string> <list>
#
# Checks if string is already part of string-list. If it is then just return the
# original list, otherwise return the list with the string inserted(front) /
# appended(back) to the list.
# ------------------------------------------------------------------------------
mf_cond_insert () {
local string="$1"
local list="$2"
if [ "$list" == "" ] || [ "$string" == "" ]; then # missing elements
echo "$list$string"
elif mf_in_list $string $list ; then
echo "$list"
else
echo "$string:$list"
fi
}
mf_cond_append () {
local string="$1"
local list="$2"
if [ "$list" == "" ] || [ "$string" == "" ]; then # missing elements
echo "$list$string"
elif mf_in_list $string $list ; then
echo "$list"
else
echo "$list:$string"
fi
}
#-----------------------------------------------------------------------------
# mf_insert/append_if_path <path> <list>
#
# Provided that path exists then conditionally add it to the front/back of the
# path-list (path list is ":" separated). Note: need to account for being passed and
# empty list or empty path (hence the trickiness in the tests).
# ----------------------------------------------------------------------------
mf_insert_if_path () {
local path="$1"
local list="$2"
if ! [ -e "$path" ]; then
echo "$list"
else
echo $(mf_cond_insert "$path" "$list")
fi
}
mf_append_if_path () {
local path="$1"
local list="$2"
if ! [ -e "$path" ]; then
echo "$list"
else
echo $(mf_cond_append "$path" "$list")
fi
}
#----------------------------------------------------------------------
# see http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
#
# http://stackoverflow.com/questions/228544/how-to-tell-if-a-string-is-not-defined-in-a-bash-shell-script
#
#----------------------------------------------------------------------
#----------------------------------------------------------------------
# mf_which <program> - A wrapper around the which function. Returns
# empty string if false and non-empty string if true.
#----------------------------------------------------------------------
mf_which () {
echo $(which $1 2>/dev/null)
}
#----------------------------------------------------------------------
# mf_user_loggedin <username>
#
# Returns 0 (true) if the user is logged in and 1 (false) otherwise.
#----------------------------------------------------------------------
mf_user_loggedin () {
local user="$1"
if [ "$user" == "" ]; then
echo "$FUNCNAME: No user specified"
return 1
fi
local matched=$(who | awk '{print $1}' | sort | uniq | grep "$user")
if [ "$matched" == "$user" ]; then
return 0
fi
return 1
}
#----------------------------------------------------------------------
# mf_is_remote_shell
#
# Returns 0 (true) if this is a remote shell.
#----------------------------------------------------------------------
mf_is_remote_shell () {
if [ "$SSH_CLIENT" != "" ]; then
local ip=$(echo "$SSH_CLIENT" | awk '{print $1}')
if [ "$ip" == "127.0.0.1" ]; then
return 1
fi
return 0
fi
return 1
}
#-------------------------------
# _mf_is_number
#-------------------------------
mf_is_number() {
printf '%f' "$1" &> /dev/null
}