-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCommonFunctions.sh
1942 lines (1687 loc) · 44.4 KB
/
CommonFunctions.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
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
# CommonFunctions.sh
#
# User-defined functions are stored in this file. If this file is edited and
# saved, run the following command to bring the changes into effect in the same
# Terminal. Alternatively, this file can be sourced from ${HOME}/.bashrc so
# that changes are in automatically effect every time a new Terminal is opened.
#
# The variables whose scope is limited to the definition of the function they
# are part of is declared in lowercase alphabets.
#
# source CommonFunctions.sh
# Exit/Error/Return codes (64 - 113)
# Number of arguments, input validation and files (64 - 69)
export E_SUCCESS=0
export E_ARGS=64
export E_INPUT=65
export E_ENTITY_MISSING=66
export E_ENTITY_ZERO_SIZE=67
export E_ENTITY_USELESS=68
export E_ENTITY_PERMISSION=69
#
# Software installation/compilation (70 - 79)
export E_EASYBUILD=70
export E_COMPILE=71
export E_COMPRESS=72
export E_UNCOMPRESS=72
export E_CONFIGURE=73
export E_EXECUTE=74
export E_INSTALL=75
export E_MAKE=76
export E_MISSING_CMD=77
export E_MISSING_LIB=78
export E_INTEGRITY=79
#
# LaTeX and gnuplot (80 - 89)
export E_BIBTEX=80
export E_DVIPDF=81
export E_DVIPS=82
export E_LATEX=83
export E_MKINDEX=84
export E_PDFLATEX=85
export E_PS2PDF=86
export E_GNUPLOT=87
#
# Git (90 - 99)
export E_GIT_ADD=90
export E_GIT_BRANCH=91
export E_GIT_CHECKOUT=92
export E_GIT_CLONE=93
export E_GIT_COMMIT=94
export E_GIT_FETCH=95
export E_GIT_PULL=96
export E_GIT_PUSH=97
export E_GIT_REPO=98
export E_GIT_RM=99
export E_GIT_STATUS=100
export E_GIT_TAG=101
#
# Miscellaneous (105 - 113)
export E_DUPLICATE=109
export E_PATH=110
export E_PING=112
export E_LOCK=112
export E_ROOT=113
## BEGIN: hello, world!
# hello_world()
# Print "Hello, World" and a user-supplied string
function hello_world() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} STRING"
echo " e.g.: ${FUNCNAME} eleanor"
echo " ${FUNCNAME} george"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local user_name="$1"
# Print the result
echo
echo " Hello, World."
echo " Username is ${user_name}"
echo
}
export -f hello_world
## END: hello, world!
## BEGIN: String operations
# validate_alphabet()
# Checks if a string contains only alphabetic characters
function validate_alphabet() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} ALPHABETICAL_STRING"
echo " e.g.: ${FUNCNAME} Eleanor"
echo " ${FUNCNAME} George"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Regular expression pattern for alphabets
# ^ : beginning of the string
# [a-zA-Z]+ : one or more occurrences of a-z or A-Z
# $ : end of the string
local regexpr_pattern='^[a-zA-Z]+$'
# Check the original_string against regexpr_pattern
if [[ ${original_string} =~ ${regexpr_pattern} ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
}
export -f validate_alphabet
# validate_numeral()
# Checks if a string contains only numerals
function validate_numeral() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} NUMERAL_STRING"
echo " e.g.: ${FUNCNAME} 12345"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Regular expression pattern for numerals
# ^ : beginning of the string
# [0-9] : 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9
# + : one or more occurrence of the previous pattern (i.e., [0-9])
# $ : end of the string
local regexpr_pattern='^[0-9]+$'
# Check the original_string against the regexpr_pattern
if [[ ${original_string} =~ ${regexpr_pattern} ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
}
export -f validate_numeral
# validate_integer()
# Checks if a string contains only integers
function validate_integer() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} INTEGER"
echo " e.g.: ${FUNCNAME} 100"
echo " ${FUNCNAME} 0"
echo " ${FUNCNAME} -2000"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Regular expression pattern for integers
# ^ : beginning of the string
# [+-] : + or - sign
# ? : zero or one occurrence of the previous pattern (i.e., +/-)
# [0-9] : 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9
# + : one or more occurrence of the previous pattern (i.e., [0-9])
# $ : end of the string
local regexpr_pattern='^[+-]?[0-9]+$'
# Check the original_string against the regexpr_pattern
if [[ ${original_string} =~ ${regexpr_pattern} ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
}
export -f validate_integer
# validate_ninteger()
# Checks if a string contains only negative integers
function validate_ninteger() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} NEGATIVE_INTEGER"
echo " e.g.: ${FUNCNAME} -1"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Regular expression pattern for integers
# ^ : beginning of the string
# - : - sign
# [0-9] : 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9
# + : one or more occurrence of the previous pattern (i.e., [0-9])
# $ : end of the string
local regexpr_pattern='^-[0-9]+$'
# Check the original_string against the regexpr_pattern
if [[ ${original_string} =~ ${regexpr_pattern} ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
}
export -f validate_ninteger
# validate_pinteger()
# Checks if a string contains only positive integers
function validate_pinteger() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} POSITIVE_INTEGER"
echo " e.g.: ${FUNCNAME} 0"
echo " ${FUNCNAME} +1"
echo " ${FUNCNAME} 100"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Regular expression pattern for integers
# ^ : beginning of the string
# [+] : + sign
# ? : zero or one occurrence of the previous pattern (i.e., +)
# [0-9] : 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9
# + : one or more occurrence of the previous pattern (i.e., [0-9])
# $ : end of the string
local regexpr_pattern='^[+]?[0-9]+$'
# Check the original_string against the regexpr_pattern
if [[ ${original_string} =~ ${regexpr_pattern} ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
}
export -f validate_pinteger
# validate_float()
# Checks if a string contains only floating-point numbers
function validate_float() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} FLOATING_POINT_NUMBER"
echo " e.g.: ${FUNCNAME} 3.14159"
echo " ${FUNCNAME} 0"
echo " ${FUNCNAME} -2.71828"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Regular expression pattern for floating-point numbers
# ^ : beginning of the string
# [+-]? : zero or one occurrence of + or - sign
# [0-9]+ : one or more occurrences of [0-9]
# \.? : zero or one occurrence of '.' character
# [0-9]* : zero or more occurrences of [0-9]
# $ : end of the string
local regexpr_pattern='^[+-]?[0-9]+\.?[0-9]*$'
# Check the original_string against regexpr_pattern
if [[ ${original_string} =~ ${regexpr_pattern} ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
}
export validate_float
# validate_pfloat()
# Checks if a string contains only non-negative floating-point numbers
function validate_pfloat() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} NON_NEGATIVE_FLOATING_POINT_NUMBER"
echo " e.g.: ${FUNCNAME} 3.14159"
echo " ${FUNCNAME} 0"
echo " ${FUNCNAME} +3.14159"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Regular expression pattern for non-negative floating-point numbers
# ^ : beginning of the string
# [+]? : zero or one occurrence of + sign
# [0-9]+ : one or more occurrences of [0-9]
# \.? : zero or one occurrence of '.' character
# [0-9]* : zero or more occurrences of [0-9]
# $ : end of the string
local regexpr_pattern='^[+]?[0-9]+\.?[0-9]*$'
# Check the original_string against regexpr_pattern
if [[ ${original_string} =~ ${regexpr_pattern} ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
}
export validate_pfloat
# validate_nfloat()
# Checks if a string contains only negative floating-point numbers
function validate_nfloat() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} NEGATIVE_FLOATING_POINT_NUMBER"
echo " e.g.: ${FUNCNAME} -3.14159"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Regular expression pattern for floating-point numbers
# ^ : beginning of the string
# - : - sign
# [0-9]+ : one or more occurrences of [0-9]
# \.? : zero or one occurrence of '.' character
# [0-9]* : zero or more occurrences of [0-9]
# $ : end of the string
local regexpr_pattern='^-[0-9]+\.?[0-9]*$'
# Check the original_string against regexpr_pattern
if [[ ${original_string} =~ ${regexpr_pattern} ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
}
export validate_nfloat
# validate_alphanum()
# Checks if a string contains only alphabets and non-negative integers
function validate_alphanum() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} ALPHA_NUMERIC_STRING"
echo " e.g.: ${FUNCNAME} Eleanor1234"
echo " ${FUNCNAME} 123George"
echo " ${FUNCNAME} Deep4Blue"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Strip off all the non-alphabets and non non-negative integers from
# the original_string using sed and regular expressions
sanitized_string=$(echo "${original_string}" | sed "s/[^a-zA-Z0-9]//g")
# Compare original_string and sanitized_string, and return a value
if [ "${original_string}" != "${sanitized_string}" ]
then
return ${E_INPUT} # Invalid user input
else
return 0 # Valid user input
fi
}
export -f validate_alphanum
# validate_hostname()
# Checks if a string validates as hostname
# https://www.regexpal.com/23
function validate_hostname() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} HOSTNAME"
echo " e.g.: ${FUNCNAME} google.com"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local host_name="$1"
# Regular expression pattern for a hostname
# It must start with an alphanumeric character
# It cannot contain non-alphanumeric characters other than hyphen (-) and
# period (.)
# It must end with an alphanumeric character
# ^ : beginning of the string
# \- : - (hyphen)
# \. : . (period)
# $ : end of the string
local regexpr_pattern='^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$'
# Check the host_name against regexpr_pattern
if [[ ${host_name} =~ ${regexpr_pattern} ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
}
export -f validate_hostname
# validate_ipaddress()
# Checks if a string validates as an IP address
# https://www.linuxjournal.com/content/validating-ip-address-bash-script
function validate_ipaddress() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} IPv4_ADDRESS"
echo " e.g.: ${FUNCNAME} 172.217.8.174"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local ip_address="$1"
# Regular expression pattern for an IPv4 address
# ^ : beginning of the string
# [0-9]{1,3} : 1-3 occurrences of 0-9
# \. : . (period)
# $ : end of the string
local regexpr_pattern='^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
# Check the ip_address against regexpr_pattern
if [[ ${ip_address} =~ ${regexpr_pattern} ]]
then
# Check if each octet is between 0 and 255
# Save the built-in Internal Field Separator (IFS)
# Set the IFS to be a period
# Create an array from ip_address using the new IFS
# Restore the original value of IFS
OIFS=${IFS}
IFS="."
ip=(${ip_address})
IFS=${OIFS}
if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
else
return ${E_INPUT} # Invalid user input
fi
}
export -f validate_ipaddress
# validate_time()
# Checks if a string validates as time in human readable format (hh:mm:ss)
function validate_time() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} TIME_IN_HUMAN_READABLE_FORMAT"
echo " e.g.: ${FUNCNAME} 01:01:01"
echo " ${FUNCNAME} 12:10:05"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local time_hms="$1"
# Regular expression pattern for time in human readable format
# ^ : beginning of the string
# [0-9]{1,2} : 1-2 occurrences of 0-9
# : : : (colon)
# $ : end of the string
local regexpr_pattern='^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}$'
# Check the time_hrf against regexpr_pattern
if [[ ${time_hms} =~ ${regexpr_pattern} ]]
then
# Check if each octet is between 0 and 255
# Save the built-in Internal Field Separator (IFS)
# Set the IFS to be a period
# Create an array from ip_address using the new IFS
# Restore the original value of IFS
OIFS=${IFS}
IFS=":"
times=(${time_hms})
IFS=${OIFS}
if [[ ${times[0]} -le 24 && ${times[1]} -le 59 && ${times[2]} -le 59 ]]
then
return 0 # Valid user input
else
return ${E_INPUT} # Invalid user input
fi
else
return ${E_INPUT} # Invalid user input
fi
}
export -f validate_time
# lc2uc()
# Converts a string from lowercase to uppercase
function lc2uc() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} STRING"
echo " e.g.: ${FUNCNAME} Michigan"
echo " ${FUNCNAME} yooPER"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Parse the original string and print the result
echo "${original_string}" | tr [a-z] [A-Z]
}
export -f lc2uc
# uc2lc()
# Converts a string from uppercase to lowercase
function uc2lc() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} STRING"
echo " e.g.: ${FUNCNAME} Michigan"
echo " ${FUNCNAME} yooPER"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Parse the original string and print the result
echo "${original_string}" | tr [A-Z] [a-z]
}
export -f uc2lc
# ltrim()
# Removes leading white space from a string
function ltrim() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} STRING"
echo " e.g.: ${FUNCNAME} \" Michigan\""
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Parse the original string and print the result
echo "${original_string}" | sed "s/^[ \t]*//"
}
export -f ltrim
# rtrim()
# Removes trailing white space from a string
function rtrim() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} STRING"
echo " e.g.: ${FUNCNAME} \"Michigan \""
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Parse the original string and print the result
echo "${original_string}" | sed "s/[ \t]*$//"
}
export rtrim
# trim()
# Removes leading and trailing white space from a string
function trim() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} STRING"
echo " e.g.: ${FUNCNAME} \" Michigan \""
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$1"
# Parse the original string and print the result
echo "${original_string}" | sed "s/^[ \t]*//;s/[ \t]*$//"
}
export -f trim
# ucfirst()
# Converts the first alphabet of the first world in a string to upper case
function ucfirst () {
# Argument check
if [ $# -eq 0 ]
then
echo
echo " Usage: ${FUNCNAME} STRING"
echo " e.g.: ${FUNCNAME} \"michigan is a state in the US\""
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local original_string="$@"
# Parse the original string and print the result
local first_character=${original_string:0:1}
local rest_of_the_string=${original_string:1}
local first_character_uc=$(echo "${first_character}" | tr [a-z] [A-Z])
echo "${first_character_uc}${rest_of_the_string}"
}
export -f ucfirst
# ucwords()
# Converts the first alphabet of every word in a string to upper case (i.e., title case)
function ucwords() {
# Argument check
if [ $# -eq 0 ]
then
echo
echo " Usage: ${FUNCNAME} STRING"
echo " e.g.: ${FUNCNAME} \"michigan is a state in the US\""
echo
return ${E_ARGS}
fi
for i in $@
do
i=${i,,}
echo -n "${i^} "
done
}
export -f ucwords
## END: String operations
## BEGIN: Mathematical operations
# abs()
# Prints the absolute value of an integer
# Works with BASH version 4 or higher
function abs() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} INTEGER"
echo " e.g.: ${FUNCNAME} 1234"
echo " ${FUNCNAME} -1234"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local number="$1"
# Input validation
if ! validate_integer ${number}
then
echo " ${number} is not an integer."
echo " Invalid input. Exiting."
return ${E_INPUT}
fi
# Print the result
echo -E "${number#-}"
}
export -f abs
# fabs()
# Prints the absolute value of a floating-point number
function fabs() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} FLOATING_POINT_NUMBER"
echo " e.g.: ${FUNCNAME} 3.14159"
echo " ${FUNCNAME} -2.71828"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local number="$1"
# Input validation
if ! validate_float ${number}
then
echo " ${number} is not a floating-point number."
echo " Invalid input. Exiting."
return ${E_INPUT}
fi
# Compute and print the result
# If FLOATING_POINT_NUMBER is less than 0, print the negative
# If not, print it as is
echo "${number}" | awk '{ if($1>=0) { print $1} else {print $1*(-1) }}'
}
export -f fabs
# round()
# Rounds up a given floating-point number to a specified number of decimal
# places
function round() {
# Argument check
if [ $# -ne 2 ]
then
echo
echo " Usage: ${FUNCNAME} FLOATING_POINT_NUMBER DECIMAL_PLACES"
echo " e.g.: ${FUNCNAME} 3.14159 2"
echo " ${FUNCNAME} -2.71828 3"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local number="$1"
local decimal_places="$2"
# Input validation
if ! validate_float ${number}
then
echo " ${number} is not a floating-point number."
echo " Invalid input. Exiting."
return ${E_INPUT}
fi
if ! validate_pinteger ${decimal_places}
then
echo " ${decimal_places} is not a non-negative integer."
echo " Invalid input. Exiting."
return ${E_INPUT}
fi
# Compute and print the result
echo $(printf %.${decimal_places}f $(echo "scale=${decimal_places};(((10^${decimal_places})*${number})+0.5)/(10^${decimal_places})" | bc))
}
export -f round
# ceil()
# Rounds up a given floating-point number to the next integer
function ceil() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} FLOATING_POINT_NUMBER"
echo " e.g.: ${FUNCNAME} 3.49"
echo " ${FUNCNAME} 3.51"
echo " ${FUNCNAME} -3.51"
echo " ${FUNCNAME} -3.49"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local number="$1"
# Input validation
if ! validate_float ${number}
then
echo " ${number} is not a floating-point number."
echo " Invalid input. Exiting."
return ${E_INPUT}
fi
# Compute and print the result
echo "${number}" | awk '{printf("%d\n",$0+=$0<0?0:0.9)}'
}
export -f ceil
# pi()
# Prints the value of PI for 5 decimal places
function pi() {
# Argument check
if [ $# -ne 0 ]
then
echo
echo " Usage: ${FUNCNAME}"
echo
return ${E_ARGS}
fi
# Compute and print the result
echo "1" | awk '{ printf "%0.5f\n", $1*4*atan2(1,1); }'
}
export -f pi
# sqrt()
# Square root of a non-negative number
function sqrt() {
# Argument check
if [ $# -ne 1 ]
then
echo
echo " Usage: ${FUNCNAME} NON_NEGATIVE_NUMBER"
echo " e.g.: ${FUNCNAME} 0"
echo " ${FUNCNAME} 1"
echo " ${FUNCNAME} 122.50"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local number="$1"
# Validate NON_NEGATIVE_NUMBER
if ! validate_pfloat ${number}
then
echo
echo " ${number} must be a non-negative number."
echo " Invalid input. Exiting."
echo
return ${E_ARGS}
fi
# Compute and print the result
echo "scale = 5; sqrt(${number})" | bc -l
}
export -f sqrt
# sum()
# Sums up a given sequence of numbers
function sum() {
# Argument check
if [ $# -ne 3 ]
then
echo
echo " Usage: ${FUNCNAME} BEGIN STEP/INCREMENT END"
echo " e.g.: ${FUNCNAME} 1 1 10"
echo " ${FUNCNAME} 0.10 0.10 0.50"
echo " ${FUNCNAME} 10 -1 1"
echo " ${FUNCNAME} -0.10 -0.10 -0.50"
echo
return ${E_ARGS}
fi
# Save the argument(s) in local variable(s)
local begin="$1"
local step="$2"
local end="$3"
# Input validation
if ! validate_float ${begin}
then
echo
echo " ${begin} must be a floating-point number."
echo " Invalid input. Exiting."
echo
return ${E_INPUT}
fi
if ! validate_float ${step}
then
echo
echo " ${step} must be a floating-point number."
echo " Invalid input. Exiting."
echo
return ${E_INPUT}
fi
if ! validate_float ${end}
then
echo
echo " ${end} must be a floating-point number."
echo " Invalid input. Exiting."
echo
return ${E_INPUT}
fi
if [[ ${start} -eq ${end} ]]
then
if [[ ${step} -ne ${start} ]]
then
echo
echo " END is equal to START."
echo " STEP/INCREMENT must be equal to START."
echo " Invalid input. Exiting."
echo
return ${E_INPUT}
fi
fi
if [[ ${start} -gt ${end} ]]
then
if [[ ${step} -gt ${start} ]]
then
echo
echo " END is less than START."
echo " STEP/INCREMENT must be less than or equal to START."
echo " Invalid input. Exiting."
echo
return ${E_INPUT}
fi
fi
if [[ ${start} -lt ${end} ]]
then
if [[ ${step} -lt ${start} ]]
then
echo
echo " END is greater than START."
echo " STEP/INCREMENT must be greater than or equal to START."
echo " Invalid input. Exiting."
echo
return ${E_INPUT}
fi
fi