]> git.corax.cc Git - toolbox/commitdiff
include/is: Add is_int() function
authorMatthias Kruk <m@m10k.eu>
Thu, 2 Dec 2021 03:00:24 +0000 (12:00 +0900)
committerMatthias Kruk <m@m10k.eu>
Thu, 2 Dec 2021 03:00:24 +0000 (12:00 +0900)
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

index 427e0b64befd2cfbd96b215d0c63d3e02f7b6a43..22b758cc249c4a9c31b178187a133db88b217451 100644 (file)
@@ -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"