#!/bin/bash # Desc: Checks if arg is a float #==BEGIN Define script parameters== #===BEGIN Declare local script functions=== yell() { echo "$0: $*" >&2; } # print script path and all args to stderr die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails checkFlt() { # Desc: Checks if arg is a float # Usage: checkInt arg # Input: arg: float # Output: - return code 0 (if arg is float) # - return code 1 (if arg is not float) # Example: if ! checkInt $arg; then echo "not flt"; fi; # Version: 0.0.1 # Depends: yell(), die(), bash 5.0.3 # Ref/Attrib: JDB https://stackoverflow.com/a/12643073 float regex local returnState #===Process Arg=== if [[ $# -ne 1 ]]; then die "ERROR:Invalid number of arguments:$#"; fi; RETEST1='^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$'; # Regular Expression to test if [[ ! $1 =~ $RETEST1 ]] ; then returnState="false"; else returnState="true"; fi; #===Determine function return code=== if [ "$returnState" = "true" ]; then return 0; else return 1; fi; } # Checks if arg is integer #===END Declare local script functions=== #==END Define script parameters== #==BEGIN test code== myVar1="4" ; echo "Test 1:Should succeed because int is float without decimal places or decimal."; (if checkFlt "$myVar1"; then yell "success"; else yell "fail"; fi;) & sleep 1; myVar1="4.0" ; echo "Test 2:Should succeed because \"4.0\" is a float."; (if checkFlt "$myVar1"; then yell "success"; else yell "fail"; fi;) & sleep 1; myVar1=".0" ; echo "Test 3:Should succeed even if float lack whole numbers left of decimal."; (if checkFlt "$myVar1"; then yell "success"; else yell "fail"; fi;) & sleep 1; myVar1="4." ; echo "Test 4:Should succeed even if float lacks decimal places right of decimal."; (if checkFlt "$myVar1"; then yell "success"; else yell "fail"; fi;) & sleep 1; myVar1="14.0" ; echo "Test 5:Should succeed with multiple whole numbers to left of decimal." (if checkFlt "$myVar1"; then yell "success"; else yell "fail"; fi;) & sleep 1; myVar1="." ; echo "Test 6:Should fail because neither whole numbers nor decimal places are present."; (if checkFlt "$myVar1"; then yell "success"; else yell "fail"; fi;) & sleep 1; myVar1="4"; myVar2="5"; echo "Test 7:Should fail because multiple numbers are provided."; (if checkFlt "$myVar1" "$myVar2"; then yell "success"; else yell "fail"; fi;) & sleep 1; myVar1="4 5"; echo "Test 8:Should fail because multiple numbers are provided."; (if checkFlt "$myVar1"; then yell "success"; else yell "fail"; fi;) & sleep 1; myVar1="4.4.4"; echo "Test 9:Should fail because a float should contain only one decimal."; (if checkFlt "$myVar1"; then yell "success"; else yell "fail"; fi;) & sleep 1; myVar1="foo"; echo "Test 10:Should fail because floats should only contain numbers and decimal characters." (if checkFlt "$myVar1"; then yell "success"; else yell "fail"; fi;) & sleep 1; myVar1="foo"; myVar2="bar"; myVar3="baz"; echo "Test 11: Should fail because multiple arguments provided."; (if checkFlt "$myVar1" "$myVar2" "$myVar3"; then yell "success"; else yell "fail"; fi;) & sleep 1; #==END test code== # Author: Steven Baltakatei Sandoval # License: GPLv3+