home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!agate!doc.ic.ac.uk!uknet!rook.ukc.ac.uk!falcon.ukc.ac.uk!bnb
- From: bnb@ukc.ac.uk (The Impossible Dreamer)
- Newsgroups: comp.unix.shell
- Subject: Re: Integers in SH
- Message-ID: <6628@falcon.ukc.ac.uk>
- Date: 12 Dec 92 17:13:08 GMT
- Sender: bnb@ukc.ac.uk
- Organization: Computing Lab, University of Kent at Canterbury, UK.
- Lines: 33
- Nntp-Posting-Host: falcon.ukc.ac.uk
-
-
- The following should tell you if a given string is an positive integer on
- not, I have written it as a shell function. If your sh hasn't got shell
- functions it shouldn't be too hard to convert it...
-
- isint()
- {
- # Shell function to return true if give number is an integer
- for a in `echo $1|sed 's/./& /g'`
- do
- case $a in
- 0|1|2|3|4|5|6|7|8|9) ;;
- *) # This isn't a number.. so its not an int
- return 1 ;;
- esac
- done
- return 0;
- }
-
- To use it just put it at the top of your shell script and call it in an if
- eg.
-
- echo -n "Number of lines:"
- read a
- if isint $a
- then
- echo OK
- else
- echo Hows about an integer
- fi
- --
- Brian Blackmore, Darwin College,
- The University of Kent at Canterbury, United Kingdom.
-