From b947b1f1349bea68eb9dc2715fad40babecc274d Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Wed, 28 Jul 2021 05:47:07 +0000 Subject: [PATCH] feat(unitproc/bktemp-checkInt):Add integer check function --- unitproc/bktemp-checkInt | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 unitproc/bktemp-checkInt 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+ -- 2.30.2