home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / unix / question / 14668 < prev    next >
Encoding:
Internet Message Format  |  1992-12-14  |  1.3 KB

  1. Path: sparky!uunet!mcsun!Germany.EU.net!sbsvax!mpi-sb.mpg.de!uwe
  2. From: uwe@mpi-sb.mpg.de (Uwe Waldmann)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: Integers in SH
  5. Message-ID: <23311@sbsvax.cs.uni-sb.de>
  6. Date: 14 Dec 92 12:46:56 GMT
  7. References: <5897@esf.esf.de>
  8. Sender: news@sbsvax.cs.uni-sb.de
  9. Reply-To: uwe@mpi-sb.mpg.de
  10. Organization: Max-Planck-Institut fuer Informatik
  11. Lines: 28
  12.  
  13. In article <5897@esf.esf.de>, klaus@tat2esf.de (Klaus Wicovsky) writes:
  14. > does anyone of you out there know how I can make sure that I have read an
  15. > integer rather than a string?
  16. > Example:
  17. > #!/bin/sh
  18. > echo "How many:"
  19. > read a
  20.  
  21. case "$a" in
  22. *[!0-9]*)  echo 'not a number:' "$a" ;;
  23. *[0-9]*)   echo 'number:' "$a" ;;
  24. '')        echo 'not a number (empty):' "$a" ;;
  25. esac
  26.  
  27. This should work, provided that your sh understands the "[!...]" syntax
  28. for the complement of a character class. (Some older Bourne shells don't!
  29. In this case, you can do something similar with the expr command.)
  30. Note that the order of the cases must not be changed: You have to check
  31. _first_, whether $i contains a character that is not a digit, _then_
  32. whether it contains at least one digit (i.e., that it is nonempty).
  33.  
  34. -- 
  35. Uwe Waldmann, Max-Planck-Institut fuer Informatik
  36. Im Stadtwald, D-W6600 Saarbruecken 1, Germany
  37. Phone: +49 681 302-5431, Fax: +49 681 302-5401, E-Mail: uwe@mpi-sb.mpg.de
  38.  
  39.