| 1 | #!/bin/bash |
| 2 | # Desc: Checks if arg is a float |
| 3 | |
| 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 |
| 9 | checkFlt() { |
| 10 | # Desc: Checks if arg is a float |
| 11 | # Usage: checkFlt arg |
| 12 | # Input: arg: float |
| 13 | # Output: - return code 0 (if arg is float) |
| 14 | # - return code 1 (if arg is not float) |
| 15 | # Example: if ! checkFlt $arg; then echo "not flt"; fi; |
| 16 | # Version: 0.0.2 |
| 17 | # Depends: yell(), die(), bash 5.0.3 |
| 18 | # Ref/Attrib: JDB https://stackoverflow.com/a/12643073 float regex |
| 19 | local returnState |
| 20 | |
| 21 | #===Process Arg=== |
| 22 | if [[ $# -ne 1 ]]; then |
| 23 | die "ERROR:Invalid number of arguments:$#"; |
| 24 | fi; |
| 25 | |
| 26 | RETEST1='^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$'; # Regular Expression to test |
| 27 | if [[ ! $1 =~ $RETEST1 ]] ; then |
| 28 | returnState="false"; |
| 29 | else |
| 30 | returnState="true"; |
| 31 | fi; |
| 32 | |
| 33 | #===Determine function return code=== |
| 34 | if [ "$returnState" = "true" ]; then |
| 35 | return 0; |
| 36 | else |
| 37 | return 1; |
| 38 | fi; |
| 39 | } # Checks if arg is float |
| 40 | |
| 41 | #===END Declare local script functions=== |
| 42 | #==END Define script parameters== |
| 43 | |
| 44 | #==BEGIN test code== |
| 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; |
| 47 | |
| 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; |
| 50 | |
| 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; |
| 53 | |
| 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; |
| 56 | |
| 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; |
| 59 | |
| 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; |
| 62 | |
| 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; |
| 65 | |
| 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; |
| 68 | |
| 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; |
| 71 | |
| 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; |
| 74 | |
| 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; |
| 77 | |
| 78 | myVar1=""; echo "Test 12: Should fil because empty string."; |
| 79 | (if checkFlt "$myVar1"; then yell "success"; else yell "fail"; fi;) & sleep 1; |
| 80 | #==END test code== |
| 81 | |
| 82 | # Author: Steven Baltakatei Sandoval |
| 83 | # License: GPLv3+ |