bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "printf %d ''" should diagnose the empty string


From: Chet Ramey
Subject: Re: "printf %d ''" should diagnose the empty string
Date: Mon, 10 Feb 2025 14:31:37 -0500
User-agent: Mozilla Thunderbird

On 2/10/25 2:24 PM, Phi Debian wrote:


On Mon, Feb 10, 2025 at 5:48 PM Chet Ramey <chet.ramey@case.edu <mailto:chet.ramey@case.edu>> 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.


Would you  accept this one then, should be general enough, faster too, and explicit, really mean what is accepted as num literal

It's not a question of me `accepting' anything. It was simply a plea for
clarity. This will give you bash's idea of a number. If you disagree with
that, you'll have to expand on it.

$ echo $BASH_VERSION
5.2.21(1)-release
$ function isanum
{ (($1)) || (($1==0))  && return 0
   return 1
}  >/dev/null 2>&1

You might want to check for an empty string first, or a missing argument,
depending on your view about whether or not an empty string is equivalent
to 0.


--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]