home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / question / 15963 < prev    next >
Encoding:
Text File  |  1993-01-27  |  1.8 KB  |  52 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!inmos!fulcrum!bham!warwick!pipex!mfmail!dnh
  3. From: dnh@mfltd.co.uk (Des Herriott)
  4. Subject: Re: How to recognize a csh var as a number?
  5. Message-ID: <1993Jan27.111224.1263@mfltd.co.uk>
  6. Sender: dnh@mfltd.co.uk (Des Herriott)
  7. Organization: Micro Focus Ltd., Newbury, UK
  8. References: <1jn3t3INN88b@haydn.crhc.uiuc.edu> <1993Jan26.231002.165645@dstos3.dsto.gov.au>
  9. Date: Wed, 27 Jan 1993 11:12:24 GMT
  10. Lines: 40
  11.  
  12.  
  13. In article <1993Jan26.231002.165645@dstos3.dsto.gov.au>, sct@tumtum (Shaun Troedson) writes:
  14. > Timothy Tsai (ttsai@crhc.uiuc.edu) wrote:
  15. > : I am reading string values into a csh variable.  Depending on whether each
  16. > : string represents a number or not, I want to do certain things.  How do I
  17. > : test to see if the variable string is a number or not?
  18. > I think this works but its a mess:
  19. > echo @ var = $var | csh -t >& /dev/null
  20. > The status variable will now contain 1 if 'var' is alpha, 0 if it is num.
  21. > There's probably a less round-about way.
  22.  
  23. Well, at the risk of starting a flame war, yes, there is: use a Bourne-derived
  24. shell.  csh really is not suitable for shell programming, and only just about
  25. cuts it as a command-line interpreter.
  26.  
  27. The above problem is simple if you use sh or (and undoubtedly bash too, though 
  28. I haven't used it):
  29.  
  30. case $variable in
  31.    [0-9][0-9]*)
  32.       # it's a number
  33.       break;;
  34.    *) 
  35.       # it's not a number
  36. esac
  37.  
  38. See?  No need to fork a new shell, no need to do any output redirection.  It's
  39. all done by one process.  I can't offhand think of a way of doing that in csh
  40. (and I used csh for 3 years before I saw the light :-)
  41.  
  42. Des.
  43.  
  44. -- 
  45. Des Herriott,           /     "...and I hate, and I hate, and I hate,
  46. Micro Focus, Newbury.  /             elevator music..."
  47. +44 (0635) 565354     /
  48. dnh@mfltd.co.uk      /                   -- Tori Amos, Little Earthquakes.
  49.