home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!munnari.oz.au!yarrina.connect.com.au!warrane.connect.com.au!g2syd!georgeq
- From: georgeq@g2syd.genasys.com.au (George Quinlan)
- Subject: Re: testing if an arg is a number
- Message-ID: <1993Jan5.065746.15541@g2syd.genasys.com.au>
- Organization: Genasys II, Sydney, Australia
- References: <1hruvtINNbap@sam.ksu.ksu.edu> <6077@sixhub.UUCP>
- Date: Tue, 5 Jan 1993 06:57:46 GMT
- Lines: 41
-
- In article <6077@sixhub.UUCP> davidsen@sixhub.UUCP (bill davidsen) writes:
- >In article <1hruvtINNbap@sam.ksu.ksu.edu> dudley@sam.ksu.ksu.edu (D U D L E Y) writes:
- >| Was curious, and didn't see it in the FAQ...how would you identify if an
- >| argument to a script is *entirely* numeric?
- >|
- >| So "12" would return true while "12a" would return false.
- >|
- >| The only way I found was to use egrep.
- >
- >See also expr.
- >
- > expr "$n" : "^[0-9][0-9]*$" > /dev/null && echo numeric || echo other
- >
-
- I was recently asked a similar question and offered this solution:
-
- #!/bin/sh
-
- isnum()
- {
- echo $1 \
- | awk '\
- {
- num = $1 + 0;
-
- if (num == $1)
- exit(0);
- else
- exit(1);
- }'
- return $?
- }
-
- isnum $1 && echo "numeric" || echo "not numeric"
-
- It has the advantage that it recognises valid floating point numbers
- (including those in exponential format) as well as integers.
- --
- George Quinlan Genasys II, 33 Berry St, North Sydney, Australia.
- What is the difference between ignorance and apathy?
- I don't know, and I don't care.
-