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.
This commit is contained in:
Tom Poindexter 2019-09-23 20:59:54 -06:00 committed by GitHub
parent 460ccfdfd7
commit c3a28101e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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>&
}
```