X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/blobdiff_plain/1bb811e1d52392bf2902d8af51dbaa7ce4e10e79..b947b1f1349bea68eb9dc2715fad40babecc274d:/unitproc/bktemp-checkInt?ds=inline diff --git a/unitproc/bktemp-checkInt b/unitproc/bktemp-checkInt new file mode 100644 index 0000000..cfe396e --- /dev/null +++ b/unitproc/bktemp-checkInt @@ -0,0 +1,53 @@ +#!/bin/bash +# Desc: Checks if arg is an integer + +#==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 +checkInt() { + # Desc: Checks if arg is integer + # Usage: checkInt arg + # Input: arg: integer + # Output: - return code 0 (if arg is integer) + # - return code 1 (if arg is not integer) + # Example: if ! checkInt $arg; then echo "not int"; fi; + # Version: 0.0.1 + local returnState + + #===Process Arg=== + if [[ $# -ne 1 ]]; then + die "ERROR:Invalid number of arguments:$#"; + fi; + + RETEST1='^[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== +if checkInt 4; then yell "success"; fi; +sleep 1; +if checkInt "foo"; then yell "success"; else yell "fail"; fi; +sleep 1; +if checkInt "foo" "bar" "baz" 1; then yell "success"; else yell "fail"; fi; +sleep 1; +if checkInt; then yell "success"; else yell "fail"; fi; +#==END test code== + +# Author: Steven Baltakatei Sandoval +# License: GPLv3+