On Mon, Feb 10, 2025 at 5:48 PM Chet Ramey <[email protected]> wrote:
>
> There isn't a reward for brevity or obfuscation; say what you mean:
>
> isnum2()
> {
> case "$1" in
> [-+] | '') return 1;; # empty or bare `-' or `+'
> [-+]*[!0-9]*) return 1;; # non-digit with leading sign
> [-+]*) return 0;; # OK
> *[!0-9]*) return 1;; # non-digit
> *) return 0;; # OK
> esac
> }
>
> It obviously doesn't handle 0x constants, but could be changed to.
>
I think the point was to convert what would be seen as an octal literal by
$((...)) to something that is 10# with leading zeroes, basically date
output but not limited to.
i.e make $((i)) understand that i is signed leading 0 base 10
$ echo $BASH_VERSION
5.2.21(1)-release
$ shopt -s extglob
$ i=0012 ; echo "$((i)) $((${i/?([-+])/&10#}))"
10 12
$ i=+0012 ; echo "$((i)) $((${i/?([-+])/&10#}))"
10 12
$ i=-0012 ; echo "$((i)) $((${i/?([-+])/&10#}))"
-10 -12
As you see it does exactly what I mean, and cherry on the pie, you can even
fix 'i' for ever with
((i=${i/?([-+])/&10#}))
Now if the problem is to define is_num as a predicate, (not what I was
adressing) you noticed that you omitted 0x, but not only that but all the
base# that brings a far more complex function :-)
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
> ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRU [email protected] http://tiswww.cwru.edu/~chet/
>