diff options
author | Philip Wittamore <philip@wittamore.com> | 2025-05-16 23:44:06 +0200 |
---|---|---|
committer | Philip Wittamore <philip@wittamore.com> | 2025-05-16 23:44:06 +0200 |
commit | e39c47c631de62366153f0a4b3df3df7ead677c5 (patch) | |
tree | 59b64c3d241416714875210bf3f7172944970727 /pb | |
parent | 2b91370ea1d0c954bd1309196f3549347fcedd90 (diff) | |
download | bashlib-e39c47c631de62366153f0a4b3df3df7ead677c5.tar.gz bashlib-e39c47c631de62366153f0a4b3df3df7ead677c5.tar.bz2 bashlib-e39c47c631de62366153f0a4b3df3df7ead677c5.zip |
update
Diffstat (limited to 'pb')
-rwxr-xr-x | pb | 31 |
1 files changed, 10 insertions, 21 deletions
@@ -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 |