aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Wittamore <philip@wittamore.com>2025-05-16 23:44:06 +0200
committerPhilip Wittamore <philip@wittamore.com>2025-05-16 23:44:06 +0200
commite39c47c631de62366153f0a4b3df3df7ead677c5 (patch)
tree59b64c3d241416714875210bf3f7172944970727
parent2b91370ea1d0c954bd1309196f3549347fcedd90 (diff)
downloadbashlib-e39c47c631de62366153f0a4b3df3df7ead677c5.tar.gz
bashlib-e39c47c631de62366153f0a4b3df3df7ead677c5.tar.bz2
bashlib-e39c47c631de62366153f0a4b3df3df7ead677c5.zip
update
-rwxr-xr-xpb31
1 files changed, 10 insertions, 21 deletions
diff --git a/pb b/pb
index 3bbfe66..12a366c 100755
--- a/pb
+++ b/pb
@@ -1,24 +1,13 @@
#!/usr/bin/env bash
-# is int
-pb_isint() {
- if [[ $1 =~ ^[0-9]+$ ]] ; then
- return 0
- else
- return 1
- fi
-}
-
-# is number
-pb_isnum() {
- if [[ $1 =~ ^[+-]?([0-9]+([.][0-9]*)?|\.[0-9]+)$ ]] ; then
- return 0
- else
- return 1
- fi
-}
-
-# 256 colour functions
+#--- is number functions
+# https://stackoverflow.com/questions/806906/how-do-i-test-if-a-variable-is-a-number-in-bash/806923#806923
+pb_isuint() { case $1 in '' | *[!0-9]* ) return 1;; esac ;}
+pb_isint() { case ${1#[-+]} in '' | *[!0-9]* ) return 1;; esac ;}
+pb_isunum() { case $1 in '' | . | *[!0-9.]* | *.*.* ) return 1;; esac ;}
+pb_isnum() { case ${1#[-+]} in '' | . | *[!0-9.]* | *.*.* ) return 1;; esac ;}
+
+#--- 256 colour functions
# usage:
# source ./pb
# printf %b "$(pb_bg 196)$(pb_fg 254) RED $(pb_nc)\n"
@@ -30,7 +19,7 @@ pb_nc() {
pb_fg() {
- if [ -z "$1" ] || ! pb_isint "$1" ; then
+ if [ -z "$1" ] || ! pb_isuint "$1" ; then
return 1
fi
@@ -47,7 +36,7 @@ pb_fg() {
pb_bg() {
- if [ -z "$1" ] || ! pb_isint $1 ; then
+ if [ -z "$1" ] || ! pb_isuint $1 ; then
return 1
fi