home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / question / 14663 < prev    next >
Encoding:
Text File  |  1992-12-14  |  1.3 KB  |  43 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!spool.mu.edu!uwm.edu!linac!uchinews!machine!ddsw1!dattier
  3. From: dattier@ddsw1.mcs.com (DWT)
  4. Subject: Re: Integers in SH
  5. Message-ID: <Bz8J1p.52K@ddsw1.mcs.com>
  6. Date: Mon, 14 Dec 1992 05:53:48 GMT
  7. References: <5899@esf.esf.de>
  8. Organization: Contributor Account at ddsw1, Chicago, Illinois  60657
  9. Lines: 32
  10.  
  11. klaus@tat2.esf.de (Klaus Wicovsky) wrote in <5899@esf.esf.de>:
  12.  
  13. | does anyone of you out there know how I can make sure that I have read an
  14. | integer rather than a string?
  15. | Example:
  16. | #!/bin/sh
  17. | echo "How many:"
  18. | read a
  19. | What if someone answers "990Lines"? 
  20.  
  21. Do you mean that you want to accept "990Lines" as an answer and strip
  22. off the word or that you want to reject it because it is not an integer?
  23.  
  24. If you meant the second, there is an easy solution: a case statement based on
  25. a pattern for integers only:
  26.  
  27. #!/bin/sh
  28. echo "How many:"
  29. read a
  30. case "$a" in
  31. *[!0-9+-]*|?*[-+]*) echo not an integer ;; # has a character that is neither
  32.                                            # a digit nor a sign or has a sign
  33.                        # later than the first character
  34. *) echo Now that\'s an integer. ;;
  35. esac
  36.  
  37. David W. Tamkin   Box 59297   Northtown Station, Illinois  60659-0297
  38. dattier@ddsw1.mcs.com    CompuServe: 73720,1570    MCI Mail: 426-1818
  39.