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