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

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