home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / shell / 3821 < prev    next >
Encoding:
Text File  |  1992-09-04  |  3.1 KB  |  104 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!mcsun!sunic!ugle.unit.no!news
  3. From: Harald.Eikrem@delab.sintef.no
  4. Subject: Re: Distinguish between numeric & nonnumeric char strings in shell ?
  5. In-Reply-To: ramakris@CSGRAD.CS.VT.EDU's message of 4 Sep 92 07:02:38 GMT
  6. Message-ID: <1992Sep4.191337*Harald.Eikrem@delab.sintef.no>
  7. Sender: news@ugle.unit.no (NetNews Administrator)
  8. Organization: SINTEF DELAB, Trondheim, Norway.
  9. References: <9209040702.AA06629@csgrad.cs.vt.edu>
  10. Date: 4 Sep 92 19:13:37
  11. Lines: 91
  12.  
  13. !  How does one distinguish between a number and a nonnumeric
  14. !  character using shelltools (sh/csh, awk, sed, expr, etc) ? 
  15.  
  16. In a /bin/sh script, to test if a parameter (or a variable) truly is
  17. an integer number, you'd have to find out if that shell (/bin/sh)
  18. groks character class inversion, like:
  19.  
  20.     [!0-9]   matches any char except digits
  21.  
  22. and if it does, you can do something like:
  23.  
  24.     while :; do
  25.       case $arg in
  26.         *[!0-9]*)
  27.         echo >&2 "Argument is non-numeric"
  28.         echo -n >&2 "Reenter: "
  29.         read arg
  30.         ;;
  31.         *)    break
  32.       esac
  33.     done
  34.  
  35. If the shell does not understand character class inversion, you may
  36. still be able to perform a test like this without applying external
  37. progs (sed/awk/expr/...), but certainly it will be more cumbersome,
  38. like e.g.:
  39.  
  40.     case $arg in
  41.       *[^A-/:-~]*)
  42.         echo >&2 "Argument is non-numeric"
  43.         ......
  44.  
  45. where ^A denotes a real control-A character in the script.
  46. Or if you can't get that to work, here comes a last resort:
  47.  
  48.     case $arg in
  49.       [0-9] | [0-9][0-9] | [0-9][0-9][0-9] | [0-9][0-9][0-9][0-9] |\
  50.       [0-9][0-9][0-9][0-9][0-9] | [0-9][0-9][0-9][0-9][0-9][0-9] |\
  51.         ......
  52.         ......
  53.  
  54. You get the picture.
  55.  
  56.  
  57. !  I am trying to validate the IP address typed in by the user. 
  58. !  I isolate the four components and check each one to see if lies 
  59. !  within the legal range (0..255). In the event the user types, say, 
  60. !  tom.jack.joe.jim, I want to be able to flag an error. However the 
  61. !  IF statement, marked (+) in the fragment below, fails even if a 4 
  62. !  component string such as "tom.jack.joe.jim"  is entered. 
  63. !  Any suggestions ?
  64.  
  65. Supposing you actually want the script to decide whether it's a simple
  66. hostname, a domainified hostname or an IP (decimal) numeric address,
  67. I'd say that a test like
  68.  
  69.     case $address in
  70.       *.*.*.*.*)            # domainified hostname
  71.         ;;
  72.       [1-9]*.[0-9]*.[0-9]*.[0-9]*)    # IP numeric address
  73.         ;;
  74.       *.*)                # domainified hostname
  75.         ;;
  76.       *)                # simple hostname
  77.     esac
  78.  
  79. would suffice, because the likelihood of ever finding a true domain-
  80. name that would match the 2.nd case is none.
  81.  
  82. But of course, if you dont trust the users to know how to enter a
  83. dotted 4-digit IP address, it'll take more effort to get it checked.
  84. To check each of the IP address elements, do something like:
  85.  
  86.     saved_args=$@        # only if $@ is to be used later on
  87.  
  88.     _IFS=".$IFS"
  89.     set -- $address
  90.     IFS="$_IFS"
  91.     if [ $1 -lt 1 -o $1 -gt 223 -o $2 -gt 254 -o \
  92.          $3 -gt 254 -o $4 -lt 1 -o $4 -gt 254    ]; then
  93.         echo >&2 "Argument is not a legal IP numeric address"
  94.         ......
  95.         ......
  96.     fi
  97.  
  98.     set -- $saved_args    # if need be
  99.  
  100.  
  101. I'm not going to speak about sed/awk/<whatever> solutions.
  102.  
  103.   ~~harald E.
  104.