home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!Germany.EU.net!sbsvax!mpi-sb.mpg.de!uwe
- From: uwe@mpi-sb.mpg.de (Uwe Waldmann)
- Newsgroups: comp.unix.questions
- Subject: Re: Integers in SH
- Message-ID: <23311@sbsvax.cs.uni-sb.de>
- Date: 14 Dec 92 12:46:56 GMT
- References: <5897@esf.esf.de>
- Sender: news@sbsvax.cs.uni-sb.de
- Reply-To: uwe@mpi-sb.mpg.de
- Organization: Max-Planck-Institut fuer Informatik
- Lines: 28
-
- In article <5897@esf.esf.de>, klaus@tat2esf.de (Klaus Wicovsky) writes:
- > 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
-
- case "$a" in
- *[!0-9]*) echo 'not a number:' "$a" ;;
- *[0-9]*) echo 'number:' "$a" ;;
- '') echo 'not a number (empty):' "$a" ;;
- esac
-
- This should work, provided that your sh understands the "[!...]" syntax
- for the complement of a character class. (Some older Bourne shells don't!
- In this case, you can do something similar with the expr command.)
- Note that the order of the cases must not be changed: You have to check
- _first_, whether $i contains a character that is not a digit, _then_
- whether it contains at least one digit (i.e., that it is nonempty).
-
- --
- Uwe Waldmann, Max-Planck-Institut fuer Informatik
- Im Stadtwald, D-W6600 Saarbruecken 1, Germany
- Phone: +49 681 302-5431, Fax: +49 681 302-5401, E-Mail: uwe@mpi-sb.mpg.de
-
-