home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / question / 10720 < prev    next >
Encoding:
Internet Message Format  |  1992-09-03  |  1.7 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!linac!att!ucbvax!CSGRAD.CS.VT.EDU!ramakris
  2. From: ramakris@CSGRAD.CS.VT.EDU (S.Ramakrishnan)
  3. Newsgroups: comp.unix.questions
  4. Subject: Distinguish between numeic & nonumeric char strings in shell ?
  5. Message-ID: <9209040703.AA06681@csgrad.cs.vt.edu>
  6. Date: 4 Sep 92 07:03:57 GMT
  7. Sender: daemon@ucbvax.BERKELEY.EDU
  8. Organization: VPI&SU Computer Science Department, Blacksburg, VA
  9. Lines: 39
  10.  
  11.  
  12.  How does one distinguish between a number and a nonnumeric
  13.  character using shelltools (sh/csh, awk, sed, expr, etc) ? 
  14.  I am trying to validate the IP address typed in by the user. 
  15.  I isolate the four components and check each one to see if lies 
  16.  within the legal range (0..255). In the event the user types, say, 
  17.  tom.jack.joe.jim, I want to be able to flag an error. However the 
  18.  IF statement, marked (+) in the fragment below, fails even if a 4 
  19.  component string such as "tom.jack.joe.jim"  is entered. 
  20.  Any suggestions ?
  21.  
  22.  Encl:
  23.     #!/bin/sh
  24.         ...
  25.     while test "$ip" = ""
  26.     do
  27.           echo "    IP Address = "
  28.       read ip
  29.           if [ "$ip" != "" ]
  30.       then
  31.               cnt=`echo $ip | awk -F'.' '{ print NF }'`
  32.               one=`echo $ip | awk -F'.' '{ print $1}'`
  33.               two=`echo $ip | awk -F'.' '{ print $2}'`
  34.               thr=`echo $ip | awk -F'.' '{ print $3}'`
  35.               fou=`echo $ip | awk -F'.' '{ print $4}'`
  36. #(+)
  37.               if [ $cnt -ne 4 -o $one -gt 255 -o $two -gt 255 -o $thr -gt 255 -o $fou -gt 255 ]
  38.               then
  39.             echo ""
  40.                 echo "*** Invalid IP Address Format: Please Retype"
  41.             echo ""
  42.             ip=""
  43.               fi
  44.       fi
  45.         done
  46.     ...
  47.  
  48. ---
  49. S Ramakrishnan, CS Dept, McBryde Hall, VaTech, Virg.
  50.