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

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!zaphod.mps.ohio-state.edu!uwm.edu!spool.mu.edu!yale.edu!ira.uka.de!math.fu-berlin.de!news.netmbx.de!Germany.EU.net!mcsun!sunic!palantir.p.tvt.se!infolog!mich
  3. From: mich@lin.infolog.se (Thomas Michanek)
  4. Subject: Re: How to recognize a csh var as a number?
  5. Message-ID: <1993Jan28.122255.8354@infolog.se>
  6. Sender: news@infolog.se
  7. Organization: Infologics, Linkoping, Sweden
  8. References: <1jn3t3INN88b@haydn.crhc.uiuc.edu> <1993Jan26.231002.165645@dstos3.dsto.gov.au> <1993Jan27.111224.1263@mfltd.co.uk>
  9. Date: Thu, 28 Jan 1993 12:22:55 GMT
  10. Lines: 64
  11.  
  12. In article <1993Jan27.111224.1263@mfltd.co.uk> dnh@mfltd.co.uk (Des Herriott) writes:
  13. >
  14. >In article <1993Jan26.231002.165645@dstos3.dsto.gov.au>, sct@tumtum (Shaun Troedson) writes:
  15. >> Timothy Tsai (ttsai@crhc.uiuc.edu) wrote:
  16. >> : I am reading string values into a csh variable.  Depending on whether each
  17. >> : string represents a number or not, I want to do certain things.  How do I
  18. >> : test to see if the variable string is a number or not?
  19. >> 
  20. >
  21. >The above problem is simple if you use sh or (and undoubtedly bash too, though 
  22. >I haven't used it):
  23. >
  24. >case $variable in
  25. >   [0-9][0-9]*)
  26. >      # it's a number
  27. >      break;;
  28. >   *) 
  29. >      # it's not a number
  30. >esac
  31. >
  32. >See?  No need to fork a new shell, no need to do any output redirection.  It's
  33. >all done by one process.  I can't offhand think of a way of doing that in csh
  34. >(and I used csh for 3 years before I saw the light :-)
  35. >
  36.  
  37. OK, I'm not a sh programmer, so please don't flame me if I'm wrong, but...
  38.  
  39. 1. The above case doesn't work for single-digit numbers; it should read
  40.    case $variable in
  41.      [0-9]*)
  42.  
  43. 2. This is just as easy in csh (at least it works for me):
  44.    switch ($variable)
  45.      case [0-9]*:
  46.        # it's a number
  47.        breaksw
  48.      default:
  49.        # it's not a number
  50.    endsw
  51.   
  52. 3. Neither of the above warns for numbers followed by non-digit characters.
  53.    In csh you'll have to do:
  54.    if (`echo $variable | egrep '^[0-9]+$'` != "") then
  55.  
  56.    This forks a new shell, but it solves the problem correctly. It doesn't
  57.    use output redirection, which this one does:
  58.    echo $variable | egrep '^[0-9]+$' >& /dev/null
  59.    if ($status == 0) then
  60.  
  61. >-- 
  62. >Des Herriott,           /     "...and I hate, and I hate, and I hate,
  63. >Micro Focus, Newbury.  /             elevator music..."
  64. >+44 (0635) 565354     /
  65. >dnh@mfltd.co.uk      /                   -- Tori Amos, Little Earthquakes.
  66.  
  67. Correct me if I'm wrong...
  68.  
  69. .- - - - - - - - - - - -.  _ _  _ ___ _  _   _   _  _  _  _   .- - - - - - - -.
  70. |    Thomas Michanek    |  | |\ | |_ / \ |  / \ / _ | / `|_`  | Dial +46 13   |
  71. |  Infologics Linkoping |  | | \| |  \_/ |_,\_/ \_| | \_,._|  | 210060  Phone |
  72. | email:Thomas.Michanek |          _                          | 210068  Direct|
  73. |       @lin.infolog.se |  L I N K O P I N G  -  S W E D E N  | 210155  Fax   |
  74. `- - - - - - - - - - - -'                                     `- - - - - - - -'
  75.  
  76.