[comp.unix.questions] Can you tell if a C-shell variable is numeric?

sundaram@vx2.NYU.EDU (Vijay Sundaram) (07/17/88)

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@vx2.gba.nyu.edu
 Mathematical Sciences  |          sundaram@acf4.nyu.edu
NEW YORK UNIVERSITY     |  BITNET: vsundaram@nybvx1.bitnet
Disclaimer: The opinions expressed here are solely mine until NYU renews their
            lease.
-----------------------------------------------------------------------------
 XYZZYGLORP

rbj@nav.icst.nbs.gov (Root Boy Jim) (07/26/88)

? From: Vijay Sundaram <sundaram@VX2.GBA.NYU.EDU>

? 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.  

expr $var + 0 >& /dev/null || exit

	(Root Boy) Jim Cottrell	<rbj@icst-cmr.arpa>
	National Bureau of Standards
	Flamer's Hotline: (301) 975-5688
	The opinions expressed are solely my own
	and do not reflect NBS policy or agreement
	Careful with that VAX Eugene!