From c3a28101e48aba63e99556a114fdc3a53d37eff1 Mon Sep 17 00:00:00 2001 From: Tom Poindexter Date: Mon, 23 Sep 2019 20:59:54 -0600 Subject: [PATCH] Alternate is_int() and is_float() as one-liners Test that argument is not-null and has no leading spaces (which is otherwise acceptable to printf). Check for int or float conversion via printf. Invalid numbers or numbers with trailing spaces cause printf to fail. --- README.md | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1b8c228..8b3b100 100644 --- a/README.md +++ b/README.md @@ -820,12 +820,7 @@ var=$((var2 > var ? var2 : var)) ```sh is_float() { # Usage: is_float "number" - case $1 in - *.*.*|*[!-.0-9]*) ;; - *[0-9].[0-9]*) return 0 - esac - - return 1 + [ -n "$1" -a "$1" = "${1## }" ] && printf %f "$1" >/dev/null 2>&1 } ``` @@ -837,10 +832,7 @@ is_float() { ```sh is_int() { # usage: is_int "number" - case $1 in - *[!-0-9]*|'') return 1 ;; - *[0-9]*) - esac + [ -n "$1" -a "$1" = "${1## }" ] && printf %d "$1" >/dev/null 2>& } ```