feat(u/bktemp-checkFlt):Add bash function to check for floats
[BK-2020-03.git] / unitproc / bktemp-checkInt
1 #!/bin/bash
2 # Desc: Checks if arg is an integer
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 checkInt() {
10 # Desc: Checks if arg is integer
11 # Usage: checkInt arg
12 # Input: arg: integer
13 # Output: - return code 0 (if arg is integer)
14 # - return code 1 (if arg is not integer)
15 # Example: if ! checkInt $arg; then echo "not int"; fi;
16 # Version: 0.0.1
17 local returnState
18
19 #===Process Arg===
20 if [[ $# -ne 1 ]]; then
21 die "ERROR:Invalid number of arguments:$#";
22 fi;
23
24 RETEST1='^[0-9]+$'; # Regular Expression to test
25 if [[ ! $1 =~ $RETEST1 ]] ; then
26 returnState="false";
27 else
28 returnState="true";
29 fi;
30
31 #===Determine function return code===
32 if [ "$returnState" = "true" ]; then
33 return 0;
34 else
35 return 1;
36 fi;
37 } # Checks if arg is integer
38
39 #===END Declare local script functions===
40 #==END Define script parameters==
41
42 #==BEGIN test code==
43 if checkInt 4; then yell "success"; fi;
44 sleep 1;
45 if checkInt "foo"; then yell "success"; else yell "fail"; fi;
46 sleep 1;
47 if checkInt "foo" "bar" "baz" 1; then yell "success"; else yell "fail"; fi;
48 sleep 1;
49 if checkInt; then yell "success"; else yell "fail"; fi;
50 #==END test code==
51
52 # Author: Steven Baltakatei Sandoval
53 # License: GPLv3+