From 1dd9bb9c5c28f6cadf990d31f8e4fc4ca6e4722a Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Thu, 2 Dec 2021 12:00:24 +0900 Subject: [PATCH] include/is: Add is_int() function The is_digits() function provided by the "is" module can be used to determine if an input consists of only digits. However, it cannot be used to validate inputs that may be prefixed with a sign. This commit adds the is_int() function, which can be used to validate numeric inputs that may be prefixed with a sign. --- include/is.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/is.sh b/include/is.sh index 427e0b6..22b758c 100644 --- a/include/is.sh +++ b/include/is.sh @@ -20,6 +20,16 @@ __init() { return 0 } +is_int() { + local str="$1" + + if [[ "$str" =~ ^[+-]{,1}[0-9]+$ ]]; then + return 0 + fi + + return 1 +} + is_digits() { local str="$1" -- 2.47.3