Can you tell if a C-shell variable is numeric?

Vijay Sundaram sundaram at vx2.NYU.EDU
Sun Jul 17 11:46:00 AEST 1988


Hi gurus,

A question on the C-shell.  I'm writing a C-shell script (no flames please!)
and I need to determine if a shell variable is a number as opposed to a 
string.  

I can see yee wise old guys tellin' me, 
"If you're talkin' numerics, use C. period."
For the sake of getting on with the business, lets assume Ritchie wrote
Fortran instead.  Now don't get ideas 'cause I don't know Fortran!

My first attempt was the following kludge:

Lets say the variable is argv[1],

% @ argv[1]++

This gives me the following error if argv[1] is not numeric:
@: Expression syntax

So I did:

% @ argv[1]++ >& /dev/null      # redirected stderr for elegance(?)
% $status                     
% echo $status
1
%

This tells me that an error was made, hence argv[1] was not numeric
and I can continue with my weird application.

The problem with this horrendous kludge is that if these set of
commands were inserted in a script file, execution quits after the
first statement, so $status is never queried.

Any solution (if one exists!) is welcome.  Please restrict flames if
you can't totally avoid them, this being a most sweltering summer as
it is. :-)

Please use e-mail.

-----------------------------------------------------------------------------
Vijay Sundaram          |  UUCP:   {allegra,rocky,harvard}!cmcl2!vx2!sundaram
Courant Institute of    |  ARPA:   sundaram at vx2.gba.nyu.edu
 Mathematical Sciences  |          sundaram at acf4.nyu.edu
NEW YORK UNIVERSITY     |  BITNET: vsundaram at nybvx1.bitnet
Disclaimer: The opinions expressed here are solely mine until NYU renews their
            lease.
-----------------------------------------------------------------------------
 XYZZYGLORP



More information about the Comp.unix.questions mailing list