-
Notifications
You must be signed in to change notification settings - Fork 23
/
common.sh
executable file
·385 lines (348 loc) · 9.08 KB
/
common.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/sh
#------------------------------------------------------------------
# common.sh
#
# Common convenience routines
#
# (c) Kaiwan N Billimoria
# kaiwan -at- kaiwantech -dot- com
# License: MIT
#------------------------------------------------------------------
export TOPDIR=$(pwd)
ON=1
OFF=0
PFX=$(dirname $(which $0 2>/dev/null)) # dir in which 'procmap' and tools reside
source ${PFX}/err_common.sh || {
echo "$name: could not source ${PFX}/err_common.sh, aborting..."
exit 1
}
source ${PFX}/color.sh || {
echo "$name: could not source ${PFX}/color.sh, aborting..."
exit 1
}
prompt()
{
[ $# -gt 0 ] && echo "$@" || printf "[Enter] to continue... "
read
}
# runcmd
# Parameters
# $1 ... : params are the command to run
runcmd()
{
[ $# -eq 0 ] && return
echo "$@"
eval "$@"
}
# ref: https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable
trim() {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
printf '%s' "$var"
}
# If we're not in a GUI (X Windows) display, abort (reqd for yad)
check_gui()
{
which xdpyinfo > /dev/null 2>&1 || {
FatalError "xdpyinfo (package x11-utils) does not seem to be installed. Aborting..."
}
xdpyinfo >/dev/null 2>&1 || {
FatalError "Sorry, we're not running in a GUI display environment. Aborting..."
}
which xrandr > /dev/null 2>&1 || {
FatalError "xrandr (package x11-server-utils) does not seem to be installed. Aborting..."
}
#--- Screen Resolution stuff
res_w=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f1)
res_h=$(xrandr --current | grep '*' | uniq | awk '{print $1}' | cut -d 'x' -f2)
centre_x=$((($res_w/3)+0))
centre_y=$((($res_h/3)-100))
CAL_WIDTH=$((($res_w/3)+200))
CAL_HT=$(($res_h/3))
}
# genLogFilename
# Generates a logfile name that includes the date/timestamp
# Format:
# ddMmmYYYY[_HHMMSS]
# Parameter(s)
# #$1 : String to prefix to log filename, null okay as well [required]
# $1 : Include time component or not [required]
# $1 = 0 : Don't include the time component (only date) in the log filename
# $1 = 1 : include the time component in the log filename
genLogFilename()
{
[ $1 -eq 0 ] && log_filename=$(date +%d%b%Y)
[ $1 -eq 1 ] && log_filename=$(date +%d%b%Y_%H%M%S)
echo ${log_filename}
}
# mysudo
# Simple front end to gksudo/sudo
# Parameter(s):
# $1 : descriptive message
# $2 ... $n : command to execute
mysudo()
{
[ $# -lt 2 ] && {
#echo "Usage: mysudo "
return
}
local msg=$1
shift
local cmd="$@"
aecho "${LOGNAME}: ${msg}"
sudo --preserve-env sh -c "${cmd}"
}
# check_root_AIA
# Check whether we are running as root user; if not, exit with failure!
# Parameter(s):
# None.
# "AIA" = Abort If Absent :-)
check_root_AIA()
{
if [ `id -u` -ne 0 ]; then
Echo "Error: need to run as root! Aborting..."
exit 1
fi
}
# check_file_AIA
# Check whether the file, passed as a parameter, exists; if not, exit with failure!
# Parameter(s):
# $1 : Pathname of file to check for existence. [required]
# "AIA" = Abort If Absent :-)
# Returns: 0 on success, 1 on failure
check_file_AIA()
{
[ $# -ne 1 ] && return 1
[ ! -f $1 ] && {
Echo "Error: file \"$1\" does not exist. Aborting..."
exit 1
}
}
# check_folder_AIA
# Check whether the directory, passed as a parameter, exists; if not, exit with failure!
# Parameter(s):
# $1 : Pathname of folder to check for existence. [required]
# "AIA" = Abort If Absent :-)
# Returns: 0 on success, 1 on failure
check_folder_AIA()
{
[ $# -ne 1 ] && return 1
[ ! -d $1 ] && {
Echo "Error: folder \"$1\" does not exist. Aborting..."
exit 1
}
}
# check_folder_createIA
# Check whether the directory, passed as a parameter, exists; if not, create it!
# Parameter(s):
# $1 : Pathname of folder to check for existence. [required]
# "IA" = If Absent :-)
# Returns: 0 on success, 1 on failure
check_folder_createIA()
{
[ $# -ne 1 ] && return 1
[ ! -d $1 ] && {
Echo "Folder \"$1\" does not exist. Creating it..."
mkdir -p $1 && return 0 || return 1
}
}
# GetIP
# Extract IP address from ifconfig output
# Parameter(s):
# $1 : name of network interface (string)
# Returns: IPaddr on success, non-zero on failure
GetIP()
{
[ $# -ne 1 ] && return 1
ifconfig $1 >/dev/null 2>&1 || return 2
ifconfig $1 |grep 'inet addr'|awk '{print $2}' |cut -f2 -d':'
}
# get_yn_reply
# User's reply should be Y or N.
# Returns:
# 0 => user has answered 'Y'
# 1 => user has answered 'N'
get_yn_reply()
{
aecho -n "Type Y or N please (followed by ENTER) : "
str="${@}"
while true
do
aecho ${str}
read reply
case "$reply" in
y | yes | Y | YES ) return 0
;;
n* | N* ) return 1
;;
*) aecho "What? Pl type Y / N"
esac
done
}
# MountPartition
# Mounts the partition supplied as $1
# Parameters:
# $1 : device node of partition to mount
# $2 : mount point
# Returns:
# 0 => mount successful
# 1 => mount failed
MountPartition()
{
[ $# -ne 2 ] && {
aecho "MountPartition: parameter(s) missing!"
return 1
}
DEVNODE=$1
[ ! -b ${DEVNODE} ] && {
aecho "MountPartition: device node $1 does not exist?"
return 1
}
MNTPT=$2
[ ! -d ${MNTPT} ] && {
aecho "MountPartition: folder $2 does not exist?"
return 1
}
mount |grep ${DEVNODE} >/dev/null || {
#echo "The partition is not mounted, attempting to mount it now..."
mount ${DEVNODE} -t auto ${MNTPT} || {
wecho "Could not mount the '$2' partition!"
return 1
}
}
return 0
}
## is_kernel_thread
# Param: PID
# Returns:
# 1 if $1 is a kernel thread, 0 if not, 127 on failure.
is_kernel_thread()
{
[ $# -ne 1 ] && {
aecho "is_kernel_thread: parameter missing!" 1>&2
return 127
}
prcs_name=$(ps aux |awk -v pid=$1 '$2 == pid {print $11}')
#echo "prcs_name = ${prcs_name}"
[ -z ${prcs_name} ] && {
wecho "is_kernel_thread: could not obtain process name!" 1>&2
return 127
}
firstchar=$(echo "${prcs_name:0:1}")
#echo "firstchar = ${firstchar}"
len=${#prcs_name}
let len=len-1
lastchar=$(echo "${prcs_name:${len}:1}")
#echo "lastchar = ${lastchar}"
[ ${firstchar} = "[" -a ${lastchar} = "]" ] && return 1 || return 0
}
# trim_string_middle()
# Given a string ($1), if it's above the 'allowed' length, express it in 2
# parts seperated by the ellipse '...'
# Eg. the pathname
# /usr/lib/gnome-settings-daemon/gsd-screensaver-proxy
# becomes:
# /usr/lib/gnome-settings-d...emon/gsd-screensaver-proxy
#
# Parameters:
# $1 : string to process
# $2 : max length of final string
# Do Not echo anything other than the name here, as it's the 'return value'
trim_string_middle()
{
local NM_MAXLEN=$(($2-3)) # leave chars for '...'
local NM_MAXLEN_HALF=$((${NM_MAXLEN}/2))
local nmlen=${#1}
if [ ${nmlen} -le ${NM_MAXLEN} ]; then
echo "${1}"
else
local remlen=$((nmlen-NM_MAXLEN_HALF))
local n1=$(echo "${1}"|cut -c-${NM_MAXLEN_HALF})
local n2=$(echo "${1}"|cut -c${remlen}-)
local final="${n1}...${n2}"
printf "%s...%s" ${n1} ${n2}
fi
}
vecho()
{
[ ${VERBOSE} -eq 0 ] && return
echo "[v] $@"
}
#---------- c h e c k _ d e p s ---------------------------------------
# Checks passed packages - are they installed? (just using 'which';
# using the pkg management utils (apt/dnf/etc) would be too time consuming)
# Parameters:
# $1 : 1 => fatal error, exit
# 0 => warn only
# [.. $@ ..] : space-sep string of all packages to check
# Eg. check_deps "make perf spatch xterm"
check_deps()
{
local util needinstall=0
#report_progress
local severity=$1
shift
for util in $@
do
which ${util} > /dev/null 2>&1 || {
[ ${needinstall} -eq 0 ] && wecho "The following utilit[y|ies] or package(s) do NOT seem to be installed:"
iecho "[!] ${util}"
needinstall=1
continue
}
done
if [ ${needinstall} -eq 1 ] ; then
[ ${severity} -eq 1 ] && {
FatalError "You must first install the required package(s) shown above \
(check console and log output too) and then retry, thanks. Aborting..."
} || {
wecho "WARNING! The package(s) shown above are not present"
}
fi
} # end check_deps()
# Simple wrappers over check_deps();
# Recall, the fundamental theorem of software engineering FTSE:
# "We can solve any problem by introducing an extra level of indirection."
# -D Wheeler
# ;-)
check_deps_fatal()
{
check_deps 1 "$@"
}
check_deps_warn()
{
check_deps 0 "$@"
}
verify_utils_present()
{
[ ! -d /proc ] && FatalError "proc fs not available or not mounted? Aborting..." || true
check_deps_fatal "getconf bc make gcc kmod grep awk sed kill readlink head tail \
cut cat tac sort wc ldd file"
check_deps_warn "sudo tput ps smem"
# need yad? GUI env?
which xdpyinfo > /dev/null 2>&1 && check_deps_warn "yad" || true
# need dtc? -only on systems that use the DT
[[ -d /proc/device-tree ]] && check_deps_fatal "dtc" || true
}
## isathread
# Param: PID
# Returns:
# 1 if $1 is a (worker/child) thread of some process, 0 if it's a process by itself, 127 on failure.
isathread()
{
[ $# -ne 1 ] && {
aecho "isathread: parameter missing!" 1>&2
return 127
}
PARENT_PROCESS=${PID}
local t1=$(ps -LA|grep -w "${PID}" |head -n1)
local pid=$(echo ${t1} |cut -d' ' -f1)
local lwp=$(echo ${t1} |cut -d' ' -f2)
[[ ${pid} -eq ${lwp} ]] && return 0 || {
PARENT_PROCESS=${pid}
return 1
}
}