Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: test if a parameter is numeric



    Hi Lydgate :)

 * Lydgate <zsh@xxxxxxxxxxxxxxxxxx> dixit:
> On Sun, Feb 11, 2007 at 02:08:40AM +0100, DervishD wrote:
> >     If you can use "expr", you can do it with "expr", using simple
> > regexes. OK, not the fastest method, but should do and should do it
> > pretty fast. I use this approach in my "mobs" project. Being bulletproof
> > using almost-portable shell scripting is, at least, tricky. And probably
> > impossible, for what I've seen...
> 
> Sorry maybe I'm missing something, but if you need to match more than
> one digit, what's wrong with:
> 
> [[ "$1" == [0-9][0-9]* ]]
> 
> Which I think is more portable?

    If you are considering the second part as a regex, then the above is
definitely NON portable. It's not even POSIX.

    But the second part is not a regex, but a shell globbing pattern.
That means that the above will match something like "1234abcd", for
example, and ONLY if the file exists. Otherwise it won't match anything.

    In addition to this, if the expansion is not successful, some shells
may substitute nothing in the second part, leaving you with the invalid
construct:

    [[ "$1" == ]]

    And the "[[" is NOT portable, either, it's a zsh thing (and probably
bash has too, I don't know, but AFAIK is not POSIX/SuS).

    Raúl Núñez de Arenas Coronado

-- 
Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!



Messages sorted by: Reverse Date, Date, Thread, Author