-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bash_functions.Linux
696 lines (651 loc) · 21.2 KB
/
.bash_functions.Linux
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# vim: ft=bash noet:
! declare 2>&1 | grep -wq ^colors= && [ $BASH_VERSINFO -ge 4 ] && source $initDir/.colors
test "$debug" -gt 0 && echo "=> Running $bold${colors[blue]}$(basename ${BASH_SOURCE[0]})$normal ..."
test "$debug" -gt 0 && Echo "\n=> \${BASH_SOURCE[*]} = ${BASH_SOURCE[*]}\n"
test -r $initDir/.bash_functions.$osID_LIKE && Source $initDir/.bash_functions.$osID_LIKE # Si c'est un Linux de type Debian ou Redhat
test -r $initDir/.bash_functions.$osFamily.system && Source $initDir/.bash_functions.$osFamily.system # For system functions call via the "Sudo" function
function CA {
local locate="$(type -P locate)"
local CAToolPath=$($locate -r /CA.pl$ | grep -v /snap/ | grep ^/usr/)
if [ ! -s $CAToolPath ];then
scriptDir=$(dirname $0)
scriptDir=$(cd $scriptDir;pwd)
CAToolPath=$scriptDir/not_mine/CA.pl
fi
$CAToolPath "$@"
}
function connect2MyHiddenWifi {
wifi5GHzCapable >/dev/null && { connect2SSID sebWlan;return; } || connect2SSID sebWlan_2.4GHz
}
function connect2SSID {
local ssid=$1
test -z "$ssid" && {
echo "=> Please chose a connection among these :" >&2
nmcli con | egrep -w "802-11-wireless|wifi"
return 1
}
local wiFiInterface=$(nmcli dev | awk '/wifi\s|802-11-wireless\>/{print$1}')
local currentWifiSSID=$({ nmcli con status || nmcli con show --active;} 2>/dev/null | awk "/\<$wiFiInterface|802-11-wireless\>/"'{print$1}')
if [ "$currentWifiSSID" = $ssid ];then
echo "=> $FUNCNAME : You're already connected to <$ssid>." >&2
return 2
elif [ -n "$currentWifiSSID" ];then
nmcli con status 2>/dev/null || nmcli con show --active
fi
time nmcli con up id $ssid
echo
nmcli con status 2>/dev/null || nmcli con show --active
echo
time \dig -4 +short @resolver1.opendns.com A myip.opendns.com 2>/dev/null || time host -4 -t A myip.opendns.com resolver1.opendns.com | awk '/\<has\>/{print$NF}'
set +x
}
function df {
local df="command df"
local dfOptions="-Ph"
$df --help 2>&1 | \grep -q -- -T && dfOptions="${dfOptions}T"
LANG=C $df $dfOptions "$@" | \grep "^Filesystem" 1>&2
LANG=C $df $dfOptions "$@" | \grep -v "^Filesystem"
}
function heic2any {
local allArgsButLast="${@:1:$#-1}"
local lastArg="${@: -1}"
for heicFile in $allArgsButLast
do
convert "$heicFile" "${heicFile/heic/$lastArg}" || heif-convert "$heicFile" "${heicFile/heic/$lastArg}"
done
}
function heic2jpg {
heic2any "$@" ĵpg
}
function htm2man {
which pandoc >/dev/null || return
for file
do
prefix="${file/.*/}"
title="${prefix^^}"
echo "=> Converting $file to a manpage ..."
pandoc -s "$file" -o "$prefix.1"
gzip -9 "$prefix.1"
done
}
function https2zoomtg {
if [ $# != 1 ]
then
echo "=> Usage: $(basename $0) httpsURL" >&2
return 1
fi
zoommtgURL="$(echo "$1" | sed 's/^https:/zoommtg:/;s|/j/|/join?action=join\&confno=|;s/?pwd=/\&pwd=/')"
xdg-mime default Zoom.desktop x-scheme-handler/zoommtg
# xdg-mime query default x-scheme-handler/zoommtg
xdg-open $zoommtgURL
# echo "zoom $zoommtgURL & ..."
# zoom $zoommtgURL &
}
function hw-probeInstall {
local distribName=$(distribName)
local retCode=0
if [ $distribName = ubuntu ]; then
local ubuntuSources=/etc/apt/sources.list
grep -q universe $ubuntuSources || $sudo add-apt-repository universe -y
grep -q multiverse $ubuntuSources || $sudo add-apt-repository multiverse -y
grep -q "^deb .*unit193/inxi" /etc/apt/sources.list.d/*.list || $sudo add-apt-repository ppa:unit193/inxi -y
grep -q "^deb .*mikhailnov/hw-probe" /etc/apt/sources.list.d/*.list || $sudo add-apt-repository ppa:mikhailnov/hw-probe -y
apt-cache policy inxi | grep -q unit193/inxi || $sudo apt update
apt-cache policy hw-probe | grep -q mikhailnov/hw-probe || $sudo apt update
dpkg -l inxi | grep -q ^.i || $sudo apt install -V inxi
dpkg -l hw-probe | grep -q ^.i || $sudo apt install -V hw-probe
retCode=$?
elif [ $distribName = arch ]; then
if $sudo echo "";then
$sudo pacman -Sy
$sudo pacman -Fy
type -P fakeroot >/dev/null 2>&1 || $sudo pacman -S fakeroot
type -P strip >/dev/null 2>&1 || $sudo pacman -S binutils
if ! type -P inxi >/dev/null 2>&1;then
cd inxi >/dev/null 2>&1 || { git clone https://aur.archlinux.org/inxi.git;cd inxi; }
if git config remote.origin.url | grep -q /inxi;then
makepkg -si
retCode=$?
sync
cd ->/dev/null
else
git clone https://aur.archlinux.org/inxi.git
fi
fi
type -P make >/dev/null 2>&1 || $sudo pacman -S make
if ! type -P hw-probe >/dev/null 2>&1;then
cd hw-probe >/dev/null 2>&1 || { git clone https://aur.archlinux.org/hw-probe.git;cd hw-probe; }
if git config remote.origin.url | grep -q /hw-probe;then
sed -i "/^depends=/s/'edid-decode'//" PKGBUILD
makepkg -si
retCode=$?
sync
cd ->/dev/null
else
git clone https://aur.archlinux.org/hw-probe.git
fi
fi
fi
fi
echo
type -P hw-probe >/dev/null 2>&1 && \hw-probe -v
return $retCode
}
function hw-probeInstallFromSource {
local distribName=$(distribName)
local hw_probeGitREPO=https://github.com/linuxhw/hw-probe
if type -P hw-probe >/dev/null 2>&1; then
echo "=> INFO [$FUNCNAME] : hw-probe is already installed." 1>&2
return 1
fi
if ! type -P git > /dev/null 2>&1; then
echo "=> ERROR [$FUNCNAME] : You must first install <git>." 1>&2
return 2
fi
local prevDIR=$PWD
local retCode=0
mkdir -p ~/git/linuxhw
if cd ~/git/linuxhw;then
git clone $hw_probeGitREPO
if cd $(basename $hw_probeGitREPO);then
git pull
$sudo make install prefix=/usr/local
retCode=$?
echo
fi
fi
sync
cd $prevDIR
type -P hw-probe >/dev/null 2>&1 && \hw-probe -v
return $retCode
}
function ifdown {
local ifdown=$(type -P ifdown)
if [ -z "$ifdown" ];then
command ifdown
return
fi
local outputInterface=$(ip route | awk '/^default/{print$5}')
if [ $# == 0 ] || [ $1 != "$outputInterface" ] || tty | \grep /dev/tty -q;
then
$ifdown "$@"
else
echo "$FUNCNAME: You must be connected to the console to stop a network interface." >&2
return 1
fi
}
function isoLabel {
for isoFile
do
blkid -o value -s LABEL "$isoFile"
done
}
function lanIP {
set -o pipefail
local ip="command ip"
if $ip -4 -brief addr >/dev/null 2>&1;then
$ip -4 -brief addr show "$@"
else
$ip -4 -o addr show "$@" | awk '{print$2" "$4}' | column -t
fi
test $? != 0 && $ip addr show help
set +o pipefail
}
function lanIPv6 {
set -o pipefail
local ip="command ip"
if $ip -6 -brief addr >/dev/null 2>&1;then
$ip -6 -brief addr show "$@"
else
$ip -6 -o addr show "$@" | awk '{print$2" "$4}' | column -t
fi
test $? != 0 && $ip addr show help
set +o pipefail
}
function locate {
local locate="$(type -P locate)"
local locateOptions="-i --regex"
if [ $locate == "$(type -P mlocate)" ];then
locateOptions+=" -e"
groups 2>/dev/null | \egrep -w "sudo|adm|admin|root|wheel" -q || locateOptions+=" --database $HOME/.local/lib/mlocate/mlocate.db"
elif [ $locate == "$(type -P plocate)" ];then
groups 2>/dev/null | \egrep -w "sudo|adm|admin|root|wheel" -q || locateOptions+=" --database $HOME/.local/lib/plocate/plocate.db"
fi
$locate $locateOptions "$@"
}
function lsbluetooth {
\lspci | awk '/Bluetooth/{print$1}' | while read device
do
\lspci -nns $device "$@"
done
\lsusb -v 2>/dev/null | awk '/^Bus/{id=$6}/Bluetooth/{print id}' | uniq | while read device # see https://stackoverflow.com/q/69860894/5649639
do
\lsusb -d $device "$@"
done
}
function lseth {
\lspci -nnd ::200 "$@"
\lsusb | awk 'BEGIN{IGNORECASE=1}/Ethernet/{print$6}' | while read device # see https://stackoverflow.com/q/69860894/5649639
do
\lsusb -d $device "$@"
done
}
function lsgroup {
for group
do
awk -F: "/^$group:/"'{gsub(","," ");print$1":"$NF}' /etc/group
done
}
function lsprinters {
# \lspci -nnd ::280 "$@"
\lsusb -v 2>/dev/null | awk 'BEGIN{IGNORECASE=1}/^Bus/{id=$6}/Printer/{print id}' | uniq | while read device # see https://stackoverflow.com/q/69860894/5649639
do
\lsusb -d $device "$@"
done
}
function lsraid {
set -x
\lspci -nnd ::104 "$@"
\lsscsi -ss -d -x -i
\lsscsi -c # W.I.P.
\lsscsi -C -t || \lsscsi -t # W.I.P.
type -P inxi >/dev/null && \inxi -R -xxx
set +x
}
function lswebcams {
\lspci | awk 'BEGIN{IGNORECASE=1}/web.*cam|camera/{print$1}' | while read device
do
\lspci -nns $device "$@"
done
\lsusb -v 2>/dev/null | awk 'BEGIN{IGNORECASE=1}/^Bus/{id=$6}/web.*cam|camera/{print id}' | uniq | while read device # see https://stackoverflow.com/q/69860894/5649639
do
\lsusb -d $device "$@"
done
echo
v4l2-ctl --list-devices
}
function lswifiNICs {
\lspci -nnd ::280 "$@"
\lsusb -v 2>/dev/null | awk 'BEGIN{IGNORECASE=1}/^Bus/{id=$6}/WLAN|802.11|wireless [abgn]|AC1200/{print id}' | uniq | while read device # see https://stackoverflow.com/q/69860894/5649639
do
\lsusb -d $device "$@"
done
}
function lvs {
local lvs=$(type -P lvs)
$sudo $lvs -o lvname,vgname,seg_count,attr,lvsize,lv_major,lv_minor,lv_kernel_major,lv_kernel_minor,lv_uuid "$@"
}
function mac@ {
set -o pipefail
local ip="command ip"
if $ip -brief addr >/dev/null 2>&1;then
$ip -brief link show "$@"
else
$ip -o link show "$@" | awk '/link\/(ether|loopback)/{for(i=1;i<=NF;i++){if($i=="state"){status=$(i+1);break;}};sub(":","",$2);print$2" "status" "$(NF-2)" "$3}' | column -t
fi
test $? != 0 && $ip link show help
set +o pipefail
}
function man2html {
local man2html="command man2html"
$man2html -r $(man -w "$1")
}
function memUsage {
local processName=$1
local columns="pid,comm,pmem,rssize"
local ps="command ps"
if test $processName
then
\pgrep -f $processName >/dev/null && $ps -o $columns -p $(\pgrep -f $processName) | \grep PID
\pgrep -f $processName >/dev/null && $ps -o $columns -p $(\pgrep -f $processName) | sort -nrk3 | awk '/PID/;/[0-9]/{sub($4,$4/1024);print$0" MiB";total+=$4}END{if(total>1024)printf "=> Total= %.3lf GiB\n\n",total/1024>"/dev/stderr"; else printf "=> Total= %.3f MiB\n\n",total>"/dev/stderr"}' | \column -t
else
# $ps -eo rss= | awk '/[0-9]/{total+=$1/1024}END{print "\tTotal= "total" MiB"}'
\free -m | awk '/Mem:/{total=$2;used=$3}END{printf "%5.2lf%%\n", 100*used/total}'
fi
}
function mountISO {
if [ $# = 0 ];then
echo "=> $FUNCNAME isoFile" >&2
return 1
fi
isoFile="$1"
isoLabel="$(blkid -o value -s LABEL "$isoFile")"
fuseiso -p "$isoFile" "$HOME/mnt/$isoLabel/" && command df -TP "$HOME/mnt/$isoLabel/"
# loopBackDevice=$(udisksctl loop-setup -r -f "$1" | awk -F "[ .]" '{print$(NF-1)}')
# udisksctl mount -b $loopBackDevice
}
function open {
local openCommand="command xdg-open"
local fileExtension=""
[ $# = 0 ] && set -- .
for arg
do
if echo $arg | egrep 'https?://' -q;then
$openCommand "$arg"
else
ls "$arg" >/dev/null || continue
fileExtension="${arg/*./}"
if [ $fileExtension = desktop ];then
openDesktopApp=$(grep -oP "Exec=\K.*" "$arg" | sed 's/"//g;s/ %[a-zA-Z]//')
$openDesktopApp &
else
$openCommand "$arg" 2>&1 | \egrep -v "/MBuntu-Y-For-Unity/|/MacBuntu-OS.*/|/Adwaita/|QT_DEVICE_PIXEL_RATIO|QT_.*SCALE_FACTOR" &
fi
fi
done
}
function pip2Install {
if ! type -P pip2 >/dev/null;then
if [ $(distribType) = debian ];then
for pkg in "";do
:
done
fi
\wget -qO- https://bootstrap.pypa.io/pip/2.7/get-pip.py | sudo -H python2
fi
}
function pip3Install {
if ! type -P pip3 >/dev/null;then
if [ $(distribType) = debian ];then
for pkg in python3-distutils;do
dpkg -l $pkg | \grep ^i -q || { echo "=> You need to install <$pkg>";return 1; }
done
fi
wget -qO- https://bootstrap.pypa.io/get-pip.py | sudo -H python3
fi
}
function ppidof {
local regExp="$1"
\ps -o ppid= $(\pidof "$regExp")
}
function processChildren {
local -i ppid=0
test $# != 1 && {
echo "=> Usage: $FUNCNAME ppid" >&2
return 1
} || ppid=$1
if ps $ppid >/dev/null;then
echo " PID COMMAND" > $ppid.log
while ps $ppid >/dev/null
do
for pid in $(ps -o pid= --ppid $ppid)
do
\grep -qw $pid $ppid.log || ps -o pid=,command= $pid | tee -a $ppid.log
done
sleep 0.5s
done
echo "=> cat $ppid.log" >&2
cat $ppid.log
else
echo "=> WARNING: The process of pid <$ppid> is not here anymore" >&2
return 2
fi
}
function processENV_Variables {
if [ $# != 1 ] && [ $# != 2 ];then
echo "=> Usage: $FUNCNAME processName [varNAME]" >&2
return 1
fi
local processName=$1
local -i pid=0
pid=$(\pidof -s $processName)
if [ $# = 1 ];then
[ $pid != 0 ] && tr '\0' '\n' < /proc/$pid/environ | \egrep -v "^hidden=" | \grep -P "^[^\s%]+=" | sort -u
elif [ $# = 2 ];then
local varNAME=$2
[ $pid != 0 ] && tr '\0' '\n' < /proc/$pid/environ | \egrep -v "^hidden=" | \grep -P "^[^\s%]+=" | sort -u | egrep -w "$varNAME"
fi
}
function psAncestors {
local processName
test $# != 1 && {
echo "=> Usage: $FUNCNAME processName" >&2
return 1
} || processName=$1
if ps -C $processName >/dev/null;then
pstree -a -h -p -T $(pidof -s $processName) -s
else
echo "=> WARNING: The process <$processName> is not here anymore" >&2
return 2
fi
}
function psHeirs {
local processName
test $# != 1 && {
echo "=> Usage: $FUNCNAME processName" >&2
return 1
} || processName=$1
if ps -C $processName >/dev/null;then
pstree -a -h -p -T $(pidof -s $processName)
else
echo "=> WARNING: The process <$processName> is not here anymore" >&2
return 2
fi
}
function pulseaudioRestart {
pulseaudio --kill
sleep 1
\pgrep -u $USER pulseaudio >/dev/null || pulseaudio --start --log-target=journal
}
function seconds2duration {
local seconds
test $# == 1 && seconds=$1 || seconds=0
date -u +%T.%3N -d @$seconds
}
function text2man {
which txt2man >/dev/null || return
for file
do
prefix="${file/.*/}"
title="${prefix^^}"
echo "=> Converting $file to a manpage ..."
txt2man -t "$title" -s 1 "$file" | tee "$prefix.1" >/dev/null
gzip -9 "$prefix.1"
done
}
function topf {
$find . -xdev $findPrunePathsExpression -type f -size +10M -exec ls -l --block-size=M --time-style=+"%Y-%m-%d %T" {} \; 2>/dev/null | sort -nrk5 | head -n "$1" | numfmt --field 5 --from=iec --to=iec-i --suffix=B | column -t
}
function topflines {
$find "$@" -xdev $findPrunePathsExpression -type f -size +10M -exec ls -l --block-size=M --time-style=+"%Y-%m-%d %T" {} \; 2>/dev/null | sort -nrk5 | head -n $(($LINES-4)) | numfmt --field 5 --from=iec --to=iec-i --suffix=B
}
function umount.seb {
set -- ${@%/} # Remove trailing "/" from all arguments
local dir_or_device="$1"
local device mountDIR mountDIR_ROOT fsType=unknown
local df="command df"
if [ $# == 0 ]
then
echo "=> Usage: $FUNCNAME dir_or_device1" >&2
return 1
fi
if ! mount | \grep -q "$dir_or_device\s"
then
echo "=> $FUNCNAME : ERROR : $dir_or_device is not mounted." >&2
return 2
fi
if echo "$dir_or_device" | \grep -q ":";then # For NFS ?
device="$dir_or_device"
mountDIR="$(LC_MESSAGES=C $df 2>/dev/null | \grep "$dir_or_device" | awk '!/Filesystem/{$1=$2=$3=$4=$5="";gsub("^ +","");print}')"
else
echo "=> dir_or_device = $dir_or_device"
case "$dir_or_device" in
/|/dev)
echo "=> ERROR: It's forbidden to unmount <$dir_or_device>." >&2
# mountDIR="$(LC_MESSAGES=C $df "$dir_or_device" 2>/dev/null | awk '!/Filesystem/{$1=$2=$3=$4=$5="";gsub("^ +","");print}')"
return 1
;;
*)
device=$(LC_MESSAGES=C $df "$dir_or_device" | awk '!/Filesystem/{print$1}')
mountDIR="$dir_or_device"
;;
esac
fi
device=$(echo "$device" | \egrep -wq /dev/dm-[0-9]+ && { lsblk -n -o NAME "$device" | awk '{print"/dev/mapper/"$1}'; true; } || echo "$device")
fsType=$(LC_MESSAGES=C $df -a -T | grep "$device\s" | awk '!/Filesystem/{print$2}')
if [ $(blkid $device | wc -c) != 0 ];then
fsType=$(blkid $device -o value -s TYPE) # If $device is in /etc/blkid.tab
else
fsType=$(lsblk -n -o FSTYPE $device)
fi
echo "=> Unmounting $device ..." >&2
case $fsType in
iso9660) command udisksctl unmount -b $device;udisksctl loop-delete -b $device ;;
fuseblk|exfat) command umount.udisks2 "$@" || command umount.udisks "$@" || udisksctl unmount -b $device || udisksctl unmount -b $device -f || { sudo -k umount -v "$@"; } ;;
fuse.*) fusermount -u "$mountDIR";;
unknown) echo "=> $FUNCNAME : ERROR : This filesystem is of an <unknown> type." >&2; return 3;;
*) command umount -v "$@" || command umount.udisks2 "$@" || command umount.udisks "$@" || udisksctl unmount -b $device || udisksctl unmount -b $device -f || { sudo -k umount -v "$@"; } ;;
esac
rcCode=$?
mountDIR_ROOT="$(echo "$mountDIR" | cut -d/ -f-2)"
[ "$mountDIR_ROOT" = /mnt ] || [ "$mountDIR_ROOT" = /media ] && echo "=> ls $(dirname "$mountDIR") :" && ls $(dirname "$mountDIR")
set +x
return $rcCode
}
function umountISO {
if [ $# = 0 ];then
echo "=> $FUNCNAME mountDir" >&2
return 1
fi
mountDir="$1"
fusermount -u "$mountDir"
# loopBackDevice=$(sudo losetup -a | grep $1 | cut -d: -f1)
# test -z $loopBackDevice && loopBackDevice=$1
# udisksctl unmount -b $loopBackDevice
# udisksctl loop-delete -b $loopBackDevice
}
function updateGrub {
local osID=$(source /etc/os-release && echo $ID) osID_LIKE=$(source /etc/os-release && echo $ID_LIKE)
local os=$osID
if [ $osID_LIKE == debian ];then
$sudo update-grub
else
if [ -d /sys/firmware/efi ];then
[ $osID == rhel ] && os=redhat
grub-mkconfig -o /boot/efi/EFI/$os/grub.cfg
else
grub-mkconfig -o /boot/grub2/grub.cfg
fi
fi
}
function watchProcess {
local pidList=""
local ps="command ps"
mkdir -p ~/log
test $# = 1 && while true
do
pidList=$(\pgrep -f "$1")
ppidList=$($ps -o ppid= $pidList && echo)
test -n "$pidList" && ( $ps -fp $pidList && test -n "$ppidList" && echo "=> Showing the parent process :" && $ps h -fp $ppidList ) | tee -a ~/log/processSPY.log && break
sleep 0.01
done
}
function wifi5GHzCapable {
local true_OR_false=false
local wifiPhysicalDevice=$(\ls /sys/class/ieee80211 2>/dev/null | head -1)
if [ -n "$wifiPhysicalDevice" ] && type -P iw >/dev/null 2>&1 && iw phy >/dev/null 2>&1;then
iw phy $wifiPhysicalDevice info | \egrep -q '5[0-9]{3} MHz' && true_OR_false=true || true_OR_false=false
elif type -P iwlist >/dev/null 2>&1;then
iwlist $wiFiInterface frequency 2>&1 | grep "no frequency information" || iwlist $wiFiInterface frequency | \grep -q '5\..* GHz' && true_OR_false=true || true_OR_false=false
fi
echo $true_OR_false
$true_OR_false
}
function wifiCapabilities {
export LANG=C
local wifiPhysicalDevice=$(\ls /sys/class/ieee80211 2>/dev/null | head -1)
local wiFiInterface
if [ $osFamily = Darwin ]
then
echo "=> Darwin/macOS operating systems are not supported yet." >&2
return 1
fi
wiFiInterface=$(lswiFiInterfaces | head -1)
[ -z "$wiFiInterface" ] && {
set +x
echo "=> ERROR : Could not find any wireless network card." >&2
return 1
}
{
[ -n "$wifiPhysicalDevice" ] && type -P iw >/dev/null 2>&1 && echo "=> Wifi physical device capabilities :" && iw phy $wifiPhysicalDevice info
if type -P iwlist >/dev/null 2>&1;then
echo "========================================================================"
iwlist $wiFiInterface bitrate || sudo iwlist $wiFiInterface bitrate
iwlist $wiFiInterface encryption || sudo iwlist $wiFiInterface encryption
iwlist $wiFiInterface frequency
iwlist $wiFiInterface event
iwlist $wiFiInterface retry
fi
} | \less
}
function wifiInfos {
export LANG=C
local wiFiInterface
if [ $osFamily = Darwin ]
then
echo "=> Darwin/macOS operating systems are not supported yet." >&2
return 1
fi
wiFiInterface=$(lswiFiInterfaces | head -1)
[ -z "$wiFiInterface" ] && {
echo "=> ERROR : Could not find any wireless network card." >&2
return 1
}
{
if iw dev 2>/dev/null | grep -q Interface;then
echo "=> Connected Wifi information :"
iw dev $wiFiInterface info
echo "=> Station information :"
iw dev $wiFiInterface station dump
echo "=> Wifi networks list :"
wifiList 2>&1
echo "=> Connected network link status :"
iw dev $wiFiInterface link || sudo iw dev $wiFiInterface link
fi
if type -P iwlist >/dev/null 2>&1;then
echo "========================================================================"
iwlist $wiFiInterface bitrate
iwlist $wiFiInterface frequency
iwlist $wiFiInterface encryption
test "$1" = "-v" && iwlist $wiFiInterface scan | more
fi
} | \less
}
function wifiList {
local wiFiInterface=$(lswiFiInterfaces | head -1)
if [ -n "$wiFiInterface" ]; then
if type -P nmcli >/dev/null; then
nmcliVersion=$(nmcli -v | awk -F"[. ]" '/version/{printf"%d.%d%02d\n", $4, $5, $6}')
echo "=> Rescanning WiFi networks ..."
\nmcli dev wifi rescan && sleep 2
\nmcli -f ssid,bssid,mode,freq,rate,signal,security,active dev wifi list | \grep 'SSID' >&2 # Print nmcli headers
\nmcli -f ssid,bssid,mode,freq,rate,signal,security,active dev wifi list | grep -v 'SSID'
elif iw dev 2>/dev/null | \grep -q Interface;then
$sudo iw dev $wiFiInterface scan | \grep SSID
elif type -P iwlist >/dev/null 2>&1; then
$sudo iwlist $wiFiInterface scan | \grep SSID
fi
fi
}
function wifiRestart {
local -r timer=7s
local output=""
local outputDataType=""
if ! env | grep SSH_CONNECTION=;then
\nmcli nm wifi off 2>/dev/null || \nmcli radio wifi off
\nmcli nm wifi on 2>/dev/null || \nmcli radio wifi on
echo "=> Waiting $timer for Wi-Fi to connect back again ..."
sleep $timer
printf "=> WANIP: "
output="$(\curl -sA "" ipinfo.io/ip 2>/dev/null || \wget -qU "" -O- ipinfo.io/ip)"
outputDataType=$(echo "$output" | file -bi -)
case $outputDataType in
text/html*) echo "$output" | html2text;;
application/json*) echo "$output" | jq .;;
text/plain*) echo "$output";;
*) echo "Unknown data type." >&2; return 1 ;;
esac
fi
}
set +x
test "$debug" -gt 0 && echo "=> END of $bold${colors[blue]}$(basename ${BASH_SOURCE[0]})$normal"