determine if csh variable is numeric

Rigler, Stephen C. srigler at marathonoil.com
Thu Jul 14 18:40:28 UTC 2005


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 at willspc ~]$ set numval=13
> [wmcdonald at willspc ~]$ echo $numval
> 13
> [wmcdonald at willspc ~]$ echo $numval | egrep
> '[:alpha:]|[:cntrl:]|[:graph:]|[:punct:]'
> [wmcdonald at willspc ~]$ echo $?
> 1
> [wmcdonald at willspc ~]$ echo $shell
> /bin/tcsh
> [wmcdonald at 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 at garnet.acns.fsu.edu> 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 at redhat.com?subject=unsubscribe
> > https://www.redhat.com/mailman/listinfo/redhat-list
> >
> 




More information about the redhat-list mailing list