Re: determine if csh variable is numeric

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

 



Dunno how this fits on the elegant scale:

set X=1

if ( `echo $X | grep -P '^\d+$'` != "" ) then
    echo $X is a number
else
    echo $X is a not a number
endif

-Steve

On Thu, 2005-07-14 at 19:26 +0100, Will McDonald wrote:
> There's probably a more elegant way but you could echo it to grep and
> test the return value?
> 
> 
> [wmcdonald@willspc ~]$ set numval=13
> [wmcdonald@willspc ~]$ echo $numval
> 13
> [wmcdonald@willspc ~]$ echo $numval | egrep
> '[:alpha:]|[:cntrl:]|[:graph:]|[:punct:]'
> [wmcdonald@willspc ~]$ echo $?
> 1
> [wmcdonald@willspc ~]$ echo $shell
> /bin/tcsh
> [wmcdonald@willspc ~]$ 
> 
> Wrap that in some shell logic and you should be sorted. I don't really
> do csh but in sh/ksh/bash you could do something like...
> 
> if (echo $variable | egrep '[:alpha:]|[:cntrl:]|[:graph:]|[:punct:]')
> then
>    # variable's not numeric 
>    echo "Variable $variable is NOT numeric"
> else
>   # It's numeric (or null, you'll need to test for that too)
>   echo "Variable $variable is numeric."
> fi
> 
> Will.
> 
> On 14/07/05, Christopher L Judd <clj2289@xxxxxxxxxxxxxxxxxxx> wrote:
> > Would anyone know how to determine if a variable in the csh shell is
> > numeric or string?
> > 
> > Thanks,
> > Chris
> > 
> > --
> > redhat-list mailing list
> > unsubscribe mailto:redhat-list-request@xxxxxxxxxx?subject=unsubscribe
> > https://www.redhat.com/mailman/listinfo/redhat-list
> >
> 

-- 
redhat-list mailing list
unsubscribe mailto:redhat-list-request@xxxxxxxxxx?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/redhat-list

[Index of Archives]     [CentOS]     [Kernel Development]     [PAM]     [Fedora Users]     [Red Hat Development]     [Big List of Linux Books]     [Linux Admin]     [Gimp]     [Asterisk PBX]     [Yosemite News]     [Red Hat Crash Utility]


  Powered by Linux