From: Matthias Kruk Date: Thu, 2 Dec 2021 03:00:24 +0000 (+0900) Subject: include/is: Add is_int() function X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=1dd9bb9c5c28f6cadf990d31f8e4fc4ca6e4722a;p=toolbox 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. --- 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"