From e39c47c631de62366153f0a4b3df3df7ead677c5 Mon Sep 17 00:00:00 2001 From: Philip Wittamore Date: Fri, 16 May 2025 23:44:06 +0200 Subject: update --- pb | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) (limited to 'pb') 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 -- cgit v1.2.3