530ff0e48e82de964dd63c47e157a679e3311bf1
2 # Desc: Checks if arg is a float
4 #==BEGIN Define script parameters==
5 #===BEGIN Declare local script functions===
6 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
7 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
8 try
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
10 # Desc: Checks if arg is a float
13 # Output: - return code 0 (if arg is float)
14 # - return code 1 (if arg is not float)
15 # Example: if ! checkInt $arg; then echo "not flt"; fi;
17 # Depends: yell(), die(), bash 5.0.3
18 # Ref/Attrib: JDB https://stackoverflow.com/a/12643073 float regex
22 if [[ $# -ne 1 ]]; then
23 die
"ERROR:Invalid number of arguments:$#";
26 RETEST1
='^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$'; # Regular Expression to test
27 if [[ ! $1 =~
$RETEST1 ]] ; then
33 #===Determine function return code===
34 if [ "$returnState" = "true" ]; then
39 } # Checks if arg is integer
41 #===END Declare local script functions===
42 #==END Define script parameters==
45 myVar1
="4" ; echo "Test 1:Should succeed because int is float without decimal places or decimal.";
46 (if checkFlt
"$myVar1"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
48 myVar1
="4.0" ; echo "Test 2:Should succeed because \"4.0\" is a float.";
49 (if checkFlt
"$myVar1"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
51 myVar1
=".0" ; echo "Test 3:Should succeed even if float lack whole numbers left of decimal.";
52 (if checkFlt
"$myVar1"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
54 myVar1
="4." ; echo "Test 4:Should succeed even if float lacks decimal places right of decimal.";
55 (if checkFlt
"$myVar1"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
57 myVar1
="14.0" ; echo "Test 5:Should succeed with multiple whole numbers to left of decimal."
58 (if checkFlt
"$myVar1"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
60 myVar1
="." ; echo "Test 6:Should fail because neither whole numbers nor decimal places are present.";
61 (if checkFlt
"$myVar1"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
63 myVar1
="4"; myVar2
="5"; echo "Test 7:Should fail because multiple numbers are provided.";
64 (if checkFlt
"$myVar1" "$myVar2"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
66 myVar1
="4 5"; echo "Test 8:Should fail because multiple numbers are provided.";
67 (if checkFlt
"$myVar1"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
69 myVar1
="4.4.4"; echo "Test 9:Should fail because a float should contain only one decimal.";
70 (if checkFlt
"$myVar1"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
72 myVar1
="foo"; echo "Test 10:Should fail because floats should only contain numbers and decimal characters."
73 (if checkFlt
"$myVar1"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
75 myVar1
="foo"; myVar2
="bar"; myVar3
="baz"; echo "Test 11: Should fail because multiple arguments provided.";
76 (if checkFlt
"$myVar1" "$myVar2" "$myVar3"; then yell
"success"; else yell
"fail"; fi;) & sleep 1;
79 # Author: Steven Baltakatei Sandoval