mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-05-17 21:06:36 +02:00
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:
parent
460ccfdfd7
commit
c3a28101e4
12
README.md
12
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>&
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in a new issue