#!/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 must() { "$@" || 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.2 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+