chore(unitproc/bkt-checkInt):Use 'must' instead of 'try'
[BK-2020-03.git] / unitproc / bkt-checkInt
CommitLineData
b947b1f1
SBS
1#!/bin/bash
2# Desc: Checks if arg is an integer
3
4#==BEGIN Define script parameters==
5#===BEGIN Declare local script functions===
6yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
7die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
34facc2d 8must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
b947b1f1
SBS
9checkInt() {
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;
34facc2d 16 # Version: 0.0.2
b947b1f1
SBS
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
34facc2d 25 if [[ ! "$1" =~ $RETEST1 ]] ; then
b947b1f1
SBS
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==
43if checkInt 4; then yell "success"; fi;
44sleep 1;
45if checkInt "foo"; then yell "success"; else yell "fail"; fi;
46sleep 1;
47if checkInt "foo" "bar" "baz" 1; then yell "success"; else yell "fail"; fi;
48sleep 1;
49if checkInt; then yell "success"; else yell "fail"; fi;
50#==END test code==
51
52# Author: Steven Baltakatei Sandoval
53# License: GPLv3+