summaryrefslogtreecommitdiff
path: root/ndb/home/lib/funcs.sh
blob: b7d8914035efee961161e7aed01e8aee1651a18c (plain)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# NAME
#   safe, safe_eval, die, rawdie, syndie, msg, errmsg,
#   rawmsg, rawerrmsg, trace, errtrace, is_wordmatch 
#   - functions for safe execution and convenient printing and tracing
#
#   abspath - make a path absolute
# 
# SYNOPSIS
#   .  funcs.sh
#
#   is_wordmatch requires perl.
#
# DESCRIPTION
#   Funcs.sh is a collection of somewhat related functions.
#   The main categories and their respective functions are:
#     Controlled execution 		- safe, safe_eval
#     Exiting with a message		- die, rawdie, syndie 
#     Printing messages			- msg, errmsg, rawmsg, rawerrmsg
#     Tracing				- trace, errtrace
#     Pattern matching			- is_wordmatch
#   
#   
# ENVIRONMENT
#   These variables are not exported, but they are still visible
#   to, and used by, these functions.
#
#   	progname	basename of $0
#	verbose		empty or non-emtpy, used for tracing
#	synopsis	string describing the syntax of $progname 
#
# VERSION
#   2.0
#
# AUTHOR
#   Jonas Mvlsd
#   Jonas Oreland - added abspath





# Safely executes the given command and exits 
# with the given commands exit code if != 0,
# else the return value ("the functions exit 
# code") is 0. Eg: safely cd $install_dir
#
safely ()
{
    "$@"
    safely_code__=$?
    [ $safely_code__ -ne 0 ]  &&  
        { errmsg "Command failed: $@. Exit code: $safely_code__.";
	  exit $safely_code__; }
	  
    : # return "exit code" 0 from function
}




# Safely_eval executes "eval command" and exits 
# with the given commands exit code if != 0,
# else the return value (the functions "exit 
# code") is 0.
#
# Safely_eval is just like like safely, but safely_eval does 
# "eval command" instead of just "command"
#
# Safely_eval even works with pipes etc., but you have to quote
# the special characters. Eg:  safely_eval  ls \|  wc \> tst.txt 2\>\&1
#
#
safely_eval ()
{
    eval "$@"
    safely_eval_code__=$?
    [ $safely_eval_code__ -ne 0 ]  &&  
        { errmsg "Command failed: $@. Exit code: $safely_eval_code__.";
	  exit $safely_eval_code__; }
	  
    : # return "exit code" 0 from function
}






#
# safe and safe_eval are deprecated, use safely and safely_eval instead
#

# Safe executes the given command and exits 
# with the given commands exit code if != 0,
# else the return value ("the functions exit 
# code") is 0.
#
safe ()
{
    "$@"
    safe_code__=$?
    [ $safe_code__ -ne 0 ]  &&  
        { errmsg "Command failed: $@. Exit code: $safe_code__.";
	  exit $safe_code__; }
	  
    : # return "exit code" 0 from function
}




# Safe_eval executes "eval command" and exits 
# with the given commands exit code if != 0,
# else the return value (the functions "exit 
# code") is 0.
#
# Safe_eval is just like like safe, but safe_eval does 
# "eval command" instead of just "command"
#
# Safe_eval even works with pipes etc., but you have to quote
# the special characters. Eg:  safe_eval  ls \|  wc \> tst.txt 2\>\&1
#
#
safe_eval ()
{
    eval "$@"
    safe_eval_code__=$?
    [ $safe_eval_code__ -ne 0 ]  &&  
        { errmsg "Command failed: $@. Exit code: $safe_eval_code__.";
	  exit $safe_eval_code__; }
	  
    : # return "exit code" 0 from function
}






# die prints the supplied message to stderr,
# prefixed with the program name, and exits 
# with the exit code given by "-e num" or 
# 1, if no -e option is present.
#
die ()
{
        die_code__=1
	[ "X$1" = X-e ]  &&  { die_code__=$2; shift 2; }
	[ "X$1" = X-- ]  &&  shift 
	errmsg "$@"
	exit $die_code__
}



# rawdie prints the supplied message to stderr.
# It then exits with the exit code given with "-e num"
# or 1, if no -e option is present.
#
rawdie ()
{
        rawdie_code__=1
	[ "X$1" = X-e ]  &&  { rawdie_code__=$2; shift 2; }
	[ "X$1" = X-- ]  &&  shift 
	rawerrmsg "$@"
	exit $rawdie_code__
}




# Syndie prints the supplied message (if present) to stderr,
# prefixed with the program name, on the first line.
# On the second line, it prints $synopsis.
# It then exits with the exit code given with "-e num"
# or 1, if no -e option is present.
#
syndie ()
{
        syndie_code__=1
	[ "X$1" = X-e ]  &&  { syndie_code__=$2; shift 2; }
	[ "X$1" = X-- ]  &&  shift 
	[ -n "$*" ] && msg "$*"
	rawdie -e $syndie_code__  "Synopsis: $synopsis"
}




# msg prints the supplied message to stdout,
# prefixed with the program name.
#
msg ()
{
	echo "${progname:-<no program name set>}:" "$@" 
}



# msg prints the supplied message to stderr,
# prefixed with the program name.
#
errmsg ()
{
	echo "${progname:-<no program name set>}:" "$@" >&2
}



rawmsg () { echo "$*"; }  	# print the supplied message to stdout
rawerrmsg () { echo "$*" >&2; } # print the supplied message to stderr



# trace prints the supplied message to stdout if verbose is non-null
#
trace ()
{
    [ -n "$verbose" ]  &&  msg "$@"
}


# errtrace prints the supplied message to stderr if verbose is non-null
#
errtrace ()
{
    [ -n "$verbose" ]  &&  msg "$@" >&2
}



# SYNTAX  
#   is_wordmatch candidatelist wordlist
#
# DESCRIPTION
#   is_wordmatch returns true if any of the words (candidates)
#   in candidatelist is present in wordlist, otherwise it
#   returns false. 
#
# EXAMPLES
#   is_wordmatch "tuareg nixdorf low content"  "xx yy zz low fgj turn roff sd"
#   returns true, since "low" in candidatelist is present in wordlist.
#
#   is_wordmatch "tuareg nixdorf low content"  "xx yy zz slow fgj turn roff sd"
#   returns false, since none of the words in candidatelist occurs in wordlist.
#
#   is_wordmatch "tuareg nixdorf low content"  "xx yy zz low fgj tuareg roff"
#   returns true, since "low" and "tuareg" in candidatelist occurs in wordlist.
#
is_wordmatch ()
{
    is_wordmatch_pattern__=`echo $1    | 
                sed 's/^/\\\\b/;
                     s/[ 	][ 	]*/\\\\b|\\\\b/g;
                     s/$/\\\\b/;'`
    shift 
    echo "$*" | 
    perl -lne "m/$is_wordmatch_pattern__/ || exit 1" 
}

#
# abspath
#
#   Stolen from http://oase-shareware.org/shell/shelltips/script_programmer.html
#
abspath()
{
    __abspath_D=`dirname "$1"`
    __abspath_B=`basename "$1"`
    echo "`cd \"$__abspath_D\" 2>/dev/null && pwd || echo \"$__abspath_D\"`/$__abspath_B"
}

#
#
# NdbExit
#
#
NdbExit()
{
    echo "NdbExit: $1"
    exit $1
}

NdbGetExitCode()
{
    __res__=`echo $* | awk '{if($1=="NdbExit:") print $2;}'`
    if [ -n $__res__ ]
    then
	echo $__res__
    else
	echo 255
    fi
}