-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashframe.sh
More file actions
executable file
·1012 lines (911 loc) · 39.7 KB
/
bashframe.sh
File metadata and controls
executable file
·1012 lines (911 loc) · 39.7 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
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
######################################################################
# BashFrame Extended 1.5.1 #
######################################################################
#clear
# Global configuration
VERBOSE=false
DEBUG=false
DEBUG_LOG="/tmp/log/bashframe.log"
VERSION="1.5.1"
######################################################################
# 1. User and Privilege Checks #
######################################################################
# Function: IS_SUDO
# Checks if the user has root or sudo privileges
IS_SUDO() {
if [[ $EUID -eq 0 ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: User has root privileges"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: User has root privileges" >>"$DEBUG_LOG"
return 0
fi
[[ "$VERBOSE" == "true" ]] && echo "INFO: User does not have root privileges"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: User does not have root privileges" >>"$DEBUG_LOG"
return 1
}
# Function: HAS_SUDO
# Checks if the user has sudo privileges
HAS_SUDO() {
if sudo -n true 2>/dev/null; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: User has sudo privileges"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: User has sudo privileges" >>"$DEBUG_LOG"
return 0
fi
[[ "$VERBOSE" == "true" ]] && echo "INFO: User does not have sudo privileges"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: User does not have sudo privileges" >>"$DEBUG_LOG"
return 1
}
######################################################################
# 2. Tool and Installation Management #
######################################################################
# Function: IS_INSTALLED
# Checks if a package is installed
IS_INSTALLED() {
if [[ -z "$1" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No package name provided"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No package name provided" >>"$DEBUG_LOG"
return 1
fi
if command -v "$1" &>/dev/null; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: Package $1 is installed"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Package $1 is installed" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: Package $1 is not installed"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Package $1 is not installed" >>"$DEBUG_LOG"
return 1
fi
}
# Function: INSTALL
# Installs a package
# Function to install the script
INSTALL_SCRIPT() {
local SCRIPT=""
SCRIPT="$(basename "$0")"
# Check if $1 is empty
if [[ -z "$1" ]]; then
INSTALL_DIR="/usr/local/bin"
else
# Check if $1 is a valid directory
if ! [[ -d "$1" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "ERROR:INSTALL_SCRIPT Invalid directory provided"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR:INSTALL_SCRIPT Invalid directory provided" >>"$DEBUG_LOG"
return 1
fi
INSTALL_DIR="$1"
fi
# Check if user has sudo privileges if now return 1
if [[ "$EUID" -ne 0 ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO:INSTALL_SCRIPT User does not have root privileges"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO:INSTALL_SCRIPT User does not have root privileges" >>"$DEBUG_LOG"
return 1
fi
# Check if the script is already installed
if command -v "$SCRIPT" >/dev/null 2>&1; then
[[ "$VERBOSE" == "true" ]] && echo "INFO:INSTALL_SCRIPT $SCRIPT is already installed"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO:INSTALL_SCRIPT $SCRIPT is already installed" >>"$DEBUG_LOG"
return 0
fi
# install the script
cp "$0" "$INSTALL_DIR/$SCRIPT"
chmod +x "$INSTALL_DIR/$SCRIPT"
# check if the script was installed successfully
if ! command -v "$SCRIPT" >/dev/null 2>&1; then
[[ "$VERBOSE" == "true" ]] && echo "ERROR:INSTALL_SCRIPT Failed to install $SCRIPT"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR:INSTALL_SCRIPT Failed to install $SCRIPT" >>"$DEBUG_LOG"
return 1
fi
[[ "$VERBOSE" == "true" ]] && echo "INFO:INSTALL_SCRIPT $SCRIPT installed successfully"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO:INSTALL_SCRIPT $SCRIPT installed successfully" >>"$DEBUG_LOG"
return 0
}
# Function: UNINSTALL
UNINSTALL_SCRIPT() {
local SCRIPT="$1"
local INSTALL_DIR="/usr/local/bin"
# check if user has sudo privileges if now return 1
if [[ "$EUID" -ne 0 ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO:UNINSTALL_SCRIPT User does not have root privileges"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO:UNINSTALL_SCRIPT User does not have root privileges" >>"$DEBUG_LOG"
return 1
fi
#check if the script is installed
if ! command -v "$SCRIPT" >/dev/null 2>&1; then
[[ "$VERBOSE" == "true" ]] && echo "INFO:UNINSTALL_SCRIPT $SCRIPT is not installed"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO:UNINSTALL_SCRIPT $SCRIPT is not installed" >>"$DEBUG_LOG"
return 1
fi
# uninstall the script
rm "$INSTALL_DIR/$SCRIPT"
# check if the script was uninstalled successfully
if command -v "$SCRIPT" >/dev/null 2>&1; then
[[ "$VERBOSE" == "true" ]] && echo "ERROR:UNINSTALL_SCRIPT Failed to uninstall $SCRIPT"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR:UNINSTALL_SCRIPT Failed to uninstall $SCRIPT" >>"$DEBUG_LOG"
return 1
fi
[[ "$VERBOSE" == "true" ]] && echo "INFO:UNINSTALL_SCRIPT $SCRIPT uninstalled successfully"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO:UNINSTALL_SCRIPT $SCRIPT uninstalled successfully" >>"$DEBUG_LOG"
return 0
}
# Function: UPDATE
# Updates
UPDATE() {
#check if user has sudo privileges if now return 1
if [[ "$EUID" -ne 0 ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO:UPDATE User does not have root privileges"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO:UPDATE User does not have root privileges" >>"$DEBUG_LOG"
return 1
fi
#check if script is installed
if ! command -v "$SCRIPT" >/dev/null 2>&1; then
[[ "$VERBOSE" == "true" ]] && echo "INFO:UPDATE $SCRIPT is not installed"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO:UPDATE $SCRIPT is not installed" >>"$DEBUG_LOG"
return 1
fi
# update the script
[[ "$VERBOSE" == "true" ]] && echo "INFO:UPDATE Updating $SCRIPT"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO:UPDATE Updating $SCRIPT" >>"$DEBUG_LOG"
#download the latest version with curl or wget to /tmp/$SCRIPT
if command -v curl >/dev/null 2>&1; then
curl -L "$SCRIPT_URL" -o "/tmp/$SCRIPT"
elif command -v wget >/dev/null 2>&1; then
wget "$SCRIPT_URL" -O "/tmp/$SCRIPT"
else
[[ "$VERBOSE" == "true" ]] && echo "ERROR:UPDATE curl or wget not found"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR:UPDATE curl or wget not found" >>"$DEBUG_LOG"
return 1
fi
# check if the downloaded file is empty
if [ ! -s "/tmp/$SCRIPT" ]; then
[[ "$VERBOSE" == "true" ]] && echo "ERROR:UPDATE Downloaded file is empty"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR:UPDATE Downloaded file is empty" >>"$DEBUG_LOG"
return 1
fi
# move the downloaded file to /usr/local/bin
cp "/tmp/$SCRIPT" "/usr/local/bin/$SCRIPT"
chmod +x "/usr/local/bin/$SCRIPT"
[[ "$VERBOSE" == "true" ]] && echo "INFO:UPDATE $SCRIPT updated successfully"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO:UPDATE $SCRIPT updated successfully" >>"$DEBUG_LOG"
return 0
}
# Function: DOWNLOAD
DOWNLOAD() {
# Function to download files
# Usage: DOWNLOAD <URL> [DESTINATION]
local URL="$1"
local DESTINATION="$2"
if [[ -z "$URL" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No URL provided"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No URL provided" >>"$DEBUG_LOG"
return 1
fi
if [[ -z "$DESTINATION" ]]; then
DESTINATION_FILE="${URL##*/}"
DESTINATION="${PWD}/${DESTINATION_FILE}"
fi
# Check if the destination folder is writable
if ! IS_WRITABLE "$(dirname "$DESTINATION")"; then
[[ "$VERBOSE" == "true" ]] && echo "ERROR: Destination $DESTINATION is not writable"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: Destination $DESTINATION is not writable" >>"$DEBUG_LOG"
return 1
fi
[[ "$VERBOSE" == "true" ]] && echo "INFO: Downloading $URL to $DESTINATION"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Downloading $URL to $DESTINATION" >>"$DEBUG_LOG"
if command -v curl >/dev/null 2>&1; then
curl -L "$URL" -o "$DESTINATION"
STATUS=$?
elif command -v wget >/dev/null 2>&1; then
wget "$URL" -O "$DESTINATION"
STATUS=$?
else
[[ "$VERBOSE" == "true" ]] && echo "ERROR: curl or wget not found"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: curl or wget not found" >>"$DEBUG_LOG"
return 1
fi
if [[ "$STATUS" -ne 0 ]]; then
[[ "$VERBOSE" == "true" ]] && echo "ERROR: Download failed"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: Download failed" >>"$DEBUG_LOG"
return 1
fi
[[ "$VERBOSE" == "true" ]] && echo "INFO: Download successful"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Download successful" >>"$DEBUG_LOG"
return 0
}
######################################################################
# 3. User Interaction #
######################################################################
# Function: ASK_USER
# Asks the user for input
ASK_USER() {
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No question provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No question provided"
return 1
fi
[[ "$VERBOSE" == "true" ]] && echo "INFO: $1"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: $1" >>"$DEBUG_LOG"
local QUESTION="$1"
local MAX_ATTEMPTS="${2:-3}"
local ANSWER
local ATTEMPTS=0
while true; do
read -r -p "$QUESTION " ANSWER
if [[ "$ANSWER" =~ ^[Yy]$ ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: User answered yes"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: User answered yes" >>"$DEBUG_LOG"
return 0
elif [[ "$ANSWER" =~ ^[Nn]$ ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: User answered no"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: User answered no" >>"$DEBUG_LOG"
return 1
else
((ATTEMPTS++))
if [[ "$ATTEMPTS" -ge "$MAX_ATTEMPTS" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: Maximum attempts reached"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Maximum attempts reached" >>"$DEBUG_LOG"
return 1
fi
fi
done
}
######################################################################
# 4. Variable Checks #
######################################################################
# Function: IS_EMPTY
# Checks if a variable is empty
IS_EMPTY() {
if [[ -z "$1" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: Variable is empty"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Variable is empty" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: Variable is not empty"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Variable is not empty" >>"$DEBUG_LOG"
return 1
fi
}
# Function: IS_NUMERIC
# Checks if a variable is numeric
IS_NUMBER() {
# check if is empty
if [[ -z "$1" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No variable provided"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No variable provided" >>"$DEBUG_LOG"
return 1
fi
# Check if the variable is numeric
if [[ "$1" =~ ^[0-9]+$ ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: $1 is numeric"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: $1 is numeric" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: $1 is not numeric"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: $1 is not numeric" >>"$DEBUG_LOG"
return 1
fi
}
# Function: VAL_EMAIL
# Validates an email address
VAL_EMAIL() {
# Check if the email address is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No email address provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No email address provided"
return 1
fi
if [[ "$1" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: Email address is valid"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Email address is valid" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: Email address is not valid"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Email address is not valid" >>"$DEBUG_LOG"
return 1
fi
}
# Function: VAL_IP
# Validates an IP address
VAL_IP() {
# Check if the IP address is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No IP address provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No IP address provided"
return 1
fi
# Check if the IP address is valid
if [[ "$1" =~ ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: IP address is valid"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: IP address is valid" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: IP address is not valid"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: IP address is not valid" >>"$DEBUG_LOG"
return 1
fi
}
# Function: VAL_IPV6
# Validates an IPv6 address
VAL_IPV6() {
# Check if the IPv6 address is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No IPv6 address provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No IPv6 address provided"
return 1
fi
# Check if the IPv6 address is valid
if [[ "$1" =~ ^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}$ ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: IPv6 address is valid"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: IPv6 address is valid" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: IPv6 address is not valid"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: IPv6 address is not valid" >>"$DEBUG_LOG"
return 1
fi
}
# Function: VAL_URL
# Validates a URL
VAL_URL() {
# Check if the URL is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No URL provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No URL provided"
return 1
fi
# Check if the URL is valid
if [[ "$1" =~ ^(https?://)?[a-zA-Z0-9][a-zA-Z0-9.-]*\.[a-zA-Z]{2,}$ ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: URL is valid"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: URL is valid" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: URL is not valid"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: URL is not valid" >>"$DEBUG_LOG"
return 1
fi
}
# Function: VAL_MACADDR
# Validates a MAC address
VAL_MACADDR() {
# Check if the MAC address is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No MAC address provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No MAC address provided"
return 1
fi
if [[ "$1" =~ ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$ ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: MAC address is valid"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: MAC address is valid" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: MAC address is not valid"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: MAC address is not valid" >>"$DEBUG_LOG"
return 1
fi
}
######################################################################
# 5. File Management #
######################################################################
# Function: FILE_EXISTS
# Checks if a file exists
FILE_EXISTS() {
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No file provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No file provided"
return 1
fi
if [[ -f "$1" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: File $1 exists"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File $1 exists" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: File $1 does not exist"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File $1 does not exist" >>"$DEBUG_LOG"
return 1
fi
}
# Function: DIR_EXISTS
# Checks if a directory exists
DIR_EXISTS() {
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No directory provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No directory provided"
return 1
fi
if [[ -d "$1" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: Directory $1 exists"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Directory $1 exists" >>"$DEBUG_LOG"
return 0
fi
[[ "$VERBOSE" == "true" ]] && echo "INFO: Directory $1 does not exist"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Directory $1 does not exist" >>"$DEBUG_LOG"
return 1
}
# Function: FILE_EMPTY
# Checks if a file is empty
FILE_EMPTY() {
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No file provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No file provided"
return 1
fi
if [[ -s "$1" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: File $1 is not empty"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File $1 is not empty" >>"$DEBUG_LOG"
return 1
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: File $1 is empty"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File $1 is empty" >>"$DEBUG_LOG"
return 0
fi
}
# Function: DIR_EMPTY
# Checks if a directory is empty
DIR_EMPTY() {
#check if argument $1 is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No directory provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No directory provided"
return 1
fi
if [[ "$(ls -A "$1")" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: Directory $1 is not empty"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Directory $1 is not empty" >>"$DEBUG_LOG"
return 1
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: Directory $1 is empty"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: Directory $1 is empty" >>"$DEBUG_LOG"
return 0
fi
}
# Function: IS_WRITABLE
# Checks if a file or directory is writable
IS_WRITABLE() {
# check if argument $1 is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No file or directory provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No file or directory provided"
return 1
fi
# check if the file or directory is writable
if [[ -w "$1" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 is writable"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 is writable" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 is not writable"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 is not writable" >>"$DEBUG_LOG"
return 1
fi
}
# Function: IS_READABLE
# Checks if a file or directory is readable
IS_READABLE() {
# check if argument $1 is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No file or directory provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No file or directory provided"
return 1
fi
# check if the file or directory is readable
if [[ -r "$1" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 is readable"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 is readable" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 is not readable"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 is not readable" >>"$DEBUG_LOG"
return 1
fi
}
# Function: IS_EXECUTABLE
# Checks if a file or directory is executable
IS_EXECUTABLE() {
# check if argument $1 is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No file or directory provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No file or directory provided"
return 1
fi
# check if the file or directory is executable
if [[ -x "$1" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 is executable"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 is executable" >>"$DEBUG_LOG"
return 0
fi
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 is not executable"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 is not executable" >>"$DEBUG_LOG"
return 1
}
# Function: IS_OWNED_BY_USER
# Checks if a file or directory is owned by the user
IS_OWNED_BY_USER() {
# $1 = file or directory
# $2 = user
# return 0 = file or directory is owned by the user
# return 1 = file or directory is not owned by the user
# if $1 is empty, return 1
# if $2 is empty, use $USER
# if $2 is not empty, use $2
# check if argument $1 is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No file or directory provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No file or directory provided"
return 1
fi
# check if argument $2 is empty
if [[ -z "$2" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: No user provided, using $USER" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "INFO: No user provided, using $USER"
USER="${USER}"
else
USER="$2"
fi
# check if the file or directory is owned by the user
if [[ "$(stat -c "%U" "$1")" == "$USER" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 is owned by the user"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 is owned by the user $USER" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 is not owned by the user"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 is not owned by the user $USER" >>"$DEBUG_LOG"
return 1
fi
}
# Function: IS_OWNED_BY_GROUP
# Checks if a file or directory is owned by the group
IS_OWNED_BY_GROUP() {
# $1 = file or directory
# $2 = group
# return 0 = file or directory is owned by the group
# return 1 = file or directory is not owned by the group
# if $1 is empty, return 1
# if $2 is empty, use $USER
# if $2 is not empty, use $2
# check if argument $1 is empty
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No file or directory provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No file or directory provided"
return 1
fi
# check if argument $2 is empty
if [[ -z "$2" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: No group provided, using $USER" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "INFO: No group provided, using $USER"
USER="$USER"
else
USER="$2"
fi
# check if the file or directory is owned by the group
if [[ "$(stat -c "%G" "$1")" == "$USER" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 is owned by the group $USER"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 is owned by the group $USER" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 is not owned by the group"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 is not owned by the group $USER" >>"$DEBUG_LOG"
return 1
fi
}
# Function: IS_OLDER_THAN
# Checks if a file is older than X days
IS_OLDER_THAN() {
# $1 = file
# $2 = X days
# return 0 = file is older than X days
# return 1 = file is NOT older than X days
# if $1 is empty, return 1
# if $2 is empty, return 1
# if $2 is not numeric, return 1
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No file provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No file provided"
return 1
fi
if [[ -z "$2" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No days provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No days provided"
return 1
fi
if ! [[ "$2" =~ ^[0-9]+$ ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: X days is not numeric" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: X days is not numeric"
return 1
fi
# check if the file is older than X days
if [[ "$(find "$1" -mtime +"$2")" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: File $1 is older than $2 days"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File $1 is older than $2 days" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: File $1 is NOT older than $2 days"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File $1 is NOT older than $2 days" >>"$DEBUG_LOG"
return 1
fi
}
######################################################################
# 6. Logging and Debugging #
######################################################################
# Function: LOG
# Log messages to /tmp/log/$SCRIPT.log
LOG() {
# $1 = STATUS
# $2 = MESSAGE
# $3 = DEBUG_LOG
# if $1 is empty, return 1
# if $2 is empty, return 1
# if $1 is not valid, set to INFO
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: LOG() no status provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: LOG() no message provided"
return 1
fi
#check if $1 is INFO, WARNING, ERROR, DEBUG else add INFO to $1
if ! [[ "$1" =~ ^(INFO|WARNING|ERROR|DEBUG)$ ]]; then
STATUS="[INFO] : $1"
else
STATUS="$1"
#if $2 provided then DEBUG_LOG=$2
if [[ -z "$2" ]]; then
DEBUG_LOG=$2
fi
fi
MESSAGE="$2"
#check if $3 is set then DEBUG_LOG=$3
if [[ -n "$3" ]]; then
DEBUG_LOG="$3"
fi
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') [$STATUS] : $MESSAGE" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "[$STATUS] : $MESSAGE"
return 0
}
######################################################################
# 7. String Operations #
######################################################################
# Function: GENERATE_RANDOM
# Generate a random string
GENERATE_RANDOM() {
# $1 = length
# $2 = type
# return = random string
# if $1 is empty, return 1
# if $2 is empty, return 1
# if $2 is not valid, return 1
# if $1 is not numeric, return 1
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR:GENERATE_RANDOM No length provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR:GENERATE_RANDOM No length provided"
return 1
fi
if [[ -z "$2" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR:GENERATE_RANDOM No type provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR:GENERATE_RANDOM No type provided"
return 1
fi
if ! [[ "$2" =~ ^[a-zA-Z]+$ ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR:GENERATE_RANDOM Invalid type" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR:GENERATE_RANDOM Invalid type"
return 1
fi
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR:GENERATE_RANDOM Length is not numeric" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR:GENERATE_RANDOM Length is not numeric"
return 1
fi
local LENGTH=$1
local TYPE=$2
local RESULT=""
local CHARS
case "$TYPE" in
1 | "numbers")
CHARS="0123456789"
;;
2 | "characters")
CHARS="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
;;
3 | "mixed")
CHARS="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
;;
*)
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: GENERATE_RANDOM Invalid type" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: GENERATE_RANDOM Invalid type"
return 1
;;
esac
for ((i = 1; i <= LENGTH; i++)); do
RESULT="${RESULT}${CHARS:RANDOM%${#CHARS}:1}"
done
echo "$RESULT"
}
# Function: STRING_LENGTH
# Count string length
STRING_LENGTH() {
# $1 = string
# return = string length
# if $1 is empty, return 1
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No string provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No string provided"
return 1
fi
local STRING_LENGTH
STRING_LENGTH="${#1}"
[[ "$VERBOSE" == "true" ]] && echo "INFO: String length is $STRING_LENGTH"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: String length is $STRING_LENGTH" >>"$DEBUG_LOG"
echo "$STRING_LENGTH"
return 0
}
######################################################################
# 8. Network Operations #
######################################################################
# Function: GET_PUBLIC_IP
# Get public IP address
GET_PUBLIC_IP() {
#check if curl or wget or python3 is installed
#check if website returns a valid IP address else try another website
#verbse and debug mode
#return public IP address
if ! command -v curl >/dev/null 2>&1; then
if ! command -v wget >/dev/null 2>&1; then
if ! command -v python3 >/dev/null 2>&1; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: curl or wget or python3 not installed" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: curl or wget or python3 not installed"
return 1
fi
fi
fi
local PUBLIC_IP
PUBLIC_IP=$(curl -s https://api.ipify.org) || PUBLIC_IP=$(wget -qO- https://api.ipify.org) || PUBLIC_IP=$(python3 -c 'import requests; print(requests.get("https://api.ipify.org").text')
#check if public IP address is not valid then try another website
if ! VAL_IP "$PUBLIC_IP"; then
PUBLIC_IP=$(curl -s https://ifconfig.me) || PUBLIC_IP=$(wget -qO- https://ifconfig.me) || PUBLIC_IP=$(python3 -c 'import requests; print(requests.get("https://ifconfig.me").text')
fi
#check if public IP address is still not valid then try another website
if ! VAL_IP "$PUBLIC_IP"; then
PUBLIC_IP=$(curl -s https://ipecho.net/plain) || PUBLIC_IP=$(wget -qO- https://ipecho.net/plain) || PUBLIC_IP=$(python3 -c 'import requests; print(requests.get("https://ipecho.net/plain").text')
fi
echo "$PUBLIC_IP"
}
# Function: GET_LOCAL_IP
# Get local IP address
GET_LOCAL_IP() {
if command -v ifconfig >/dev/null 2>&1; then
# Try to get the IP address using ifconfig command
local IP_ADDRESS
IP_ADDRESS=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | head -n 1)
if [[ "$IP_ADDRESS" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo "$IP_ADDRESS"
return 0
fi
fi
# If neither command is available or both fail, log an error and return 1
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: Unable to get local IP address" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: Unable to get local IP address"
return 1
}
# Function: GET_MACADDR
# Get MAC address
GET_MACADDR() {
#check if ip or ifconfig is installed
#check if ip or ifconfig returns a valid MAC address
#verbse and debug mode
#return MAC address
if ! command -v ip >/dev/null 2>&1; then
if ! command -v ifconfig >/dev/null 2>&1; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: ip or ifconfig not installed" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: ip or ifconfig not installed"
return 1
fi
fi
local MACADDR
MACADDR=$(ip link show | awk '/ether/ {print $2}') || MACADDR=$(ifconfig | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
#check if MAC address is not valid then try another command
if ! VAL_MACADDR "$MACADDR"; then
MACADDR=$(ip link show | awk '/ether/ {print $2}') || MACADDR=$(ifconfig | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
fi
echo "$MACADDR"
}
# Function: CHECK_PERMISSIONS
CHECK_PERMISSIONS() {
# Function: CHECK_PERMISSIONS
# Description: Checks if a file/directory has specified permissions
# Arguments:
# $1 - Path to file or directory
# $2 - Permission to check (octal or symbolic)
# Returns:
# 0 - Has permission
# 1 - Does not have permission or error
# Examples:
# CHECK_PERMISSIONS "/etc/passwd" "644"
if [[ -z "$1" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No file or directory provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No file or directory provided"
return 1
fi
if [[ -z "$2" ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: No permission provided" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: No permission provided"
return 1
fi
if ! [[ "$2" =~ ^[0-9]+$ ]]; then
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') ERROR: Permission is not numeric" >>"$DEBUG_LOG"
[[ "$VERBOSE" == "true" ]] && echo "ERROR: Permission is not numeric"
return 1
fi
# check if the file or directory has permission
if [[ "$(stat -c "%a" "$1")" -ge "$2" ]]; then
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 has permission $2"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 has permission $2" >>"$DEBUG_LOG"
return 0
else
[[ "$VERBOSE" == "true" ]] && echo "INFO: File or directory $1 does not have permission $2"
[[ "$DEBUG" == "true" ]] && echo "$(date '+%Y-%m-%d %H:%M:%S') INFO: File or directory $1 does not have permission $2" >>"$DEBUG_LOG"
return 1
fi
}
######################################################################
# 9. Testing Functions #
######################################################################
declare -g TOTAL_TESTS=0
declare -g PASSED_TESTS=0
declare -g FAILED_TESTS=0
declare -g DEBUG_LOG="/tmp/log/test_bashframe.log"
# Assert functions
ASSERT_TRUE() {
local COMMAND="$1"
local MESSAGE="$2"
local ARGS="$3"
((TOTAL_TESTS += 1))
if eval "$COMMAND" >/dev/null 2>&1; then
echo -e "${GREEN}${BOLD}✓ PASS${NC}: $MESSAGE (${CYAN}'$ARGS'${NC}) ${GRAY}→${NC}"
echo -e " Expected: ${GREEN}${BOLD}TRUE${NC}"
echo -e " Got: ${GREEN}${BOLD}TRUE${NC}"
((PASSED_TESTS += 1))
return 0
else
echo -e "${RED}${BOLD}✗ FAIL${NC}: $MESSAGE (${CYAN}'$ARGS'${NC}) ${GRAY}→${NC}"
echo -e " Expected: ${GREEN}${BOLD}TRUE${NC}"
echo -e " Got: ${RED}${BOLD}FALSE${NC}"
((FAILED_TESTS += 1))
#return 1
exit 1
fi
}
ASSERT_FALSE() {
local COMMAND="$1"
local MESSAGE="$2"
local ARGS="$3"
let TOTAL_TESTS+=1
if ! eval "$COMMAND" >/dev/null 2>&1; then
echo -e "${GREEN}${BOLD}✓ PASS${NC}: $MESSAGE (${CYAN}'$ARGS'${NC}) ${GRAY}→${NC}"
echo -e " Expected: ${GREEN}${BOLD}FALSE${NC}"
echo -e " Got: ${GREEN}${BOLD}FALSE${NC}"
let PASSED_TESTS+=1
return 0
else
echo -e "${RED}${BOLD}✗ FAIL${NC}: $MESSAGE (${CYAN}'$ARGS'${NC}) ${GRAY}→${NC}"
echo -e " Expected: ${GREEN}${BOLD}FALSE${NC}"
echo -e " Got: ${RED}${BOLD}TRUE${NC}"
let FAILED_TESTS+=1
#return 1
exit 1
fi
}
ASSERT_EQUALS() {
local EXPECTED="$1"
local ACTUAL="$2"
local MESSAGE="$3"
let TOTAL_TESTS+=1
if [ "$EXPECTED" = "$ACTUAL" ]; then
echo -e "${GREEN}${BOLD}✓ PASS${NC}: $MESSAGE ${GRAY}→${NC}"