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

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!ftpbox!mothost!schbbs!maccvm.corp.mot.com!W10075
  3. From: W10075@maccvm.corp.mot.com (Stephen Weet)
  4. Subject: Re: Distinguish between numeric & nonnumeric char strings in shell ?
  5. Organization: Motorola
  6. Date: 4 Sep 1992 10:40:12 CDT
  7. Message-ID: <1992Sep4.155850.11074@schbbs.mot.com>
  8. Sender: news@schbbs.mot.com (Net News)
  9. Nntp-Posting-Host: maccvm.corp.mot.com
  10. Lines: 88
  11.  
  12. ------------------------- Original Article -------------------------
  13. Path:
  14. schbbs!mothost!ftpbox!uunet!wupost!sdd.hp.com!hplabs!ucbvax!CSGRAD.CS.VT.EDU!ram
  15. akris
  16. From: ramakris@CSGRAD.CS.VT.EDU (S.Ramakrishnan)
  17. Newsgroups: comp.unix.shell
  18. Subject: Distinguish between numeric & nonnumeric char strings in shell ?
  19. Message-ID: <9209040702.AA06629@csgrad.cs.vt.edu>
  20. Date: 4 Sep 92 07:02:38 GMT
  21. Sender: daemon@ucbvax.BERKELEY.EDU
  22. ------------------------- Original Article -------------------------
  23. Path:
  24. schbbs!mothost!ftpbox!uunet!wupost!sdd.hp.com!hplabs!ucbvax!CSGRAD.CS.VT.EDU!ram
  25. akris
  26. From: ramakris@CSGRAD.CS.VT.EDU (S.Ramakrishnan)
  27. Newsgroups: comp.unix.shell
  28. Subject: Distinguish between numeric & nonnumeric char strings in shell ?
  29. Message-ID: <9209040702.AA06629@csgrad.cs.vt.edu>
  30. Date: 4 Sep 92 07:02:38 GMT
  31. Sender: daemon@ucbvax.BERKELEY.EDU
  32. Organization: VPI&SU Computer Science Department, Blacksburg, VA
  33. Lines: 39
  34.  
  35.  
  36.  How does one distinguish between a number and a nonnumeric
  37.  character using shelltools (sh/csh, awk, sed, expr, etc) ?
  38.  I am trying to validate the IP address typed in by the user.
  39.  I isolate the four components and check each one to see if lies
  40.  within the legal range (0..255). In the event the user types, say,
  41.  tom.jack.joe.jim, I want to be able to flag an error. However the
  42.  IF statement, marked (+) in the fragment below, fails even if a 4
  43.  component string such as "tom.jack.joe.jim"  is entered.
  44.  Any suggestions ?
  45.  
  46.  Encl:
  47.     #!/bin/sh
  48.         ...
  49.     while test "$ip" = ""
  50.     do
  51.           echo "    IP Address = "
  52.       read ip
  53.           if [ "$ip" != "" ]
  54.       then
  55.               cnt=`echo $ip | awk -F'.' '{ print NF }'`
  56.               one=`echo $ip | awk -F'.' '{ print $1}'`
  57.               two=`echo $ip | awk -F'.' '{ print $2}'`
  58.               thr=`echo $ip | awk -F'.' '{ print $3}'`
  59.               fou=`echo $ip | awk -F'.' '{ print $4}'`
  60. #(+)
  61.               if [ $cnt -ne 4 -o $one -gt 255 -o $two -gt 255 -o $thr -gt 255 -o
  62. $fou -gt 255 ]
  63.               then
  64.             echo ""
  65.                 echo "*** Invalid IP Address Format: Please Retype"
  66.             echo ""
  67.             ip=""
  68.               fi
  69.       fi
  70.         done
  71.     ...
  72.  
  73. ---
  74. YOU MIGHT TRY SOMETHING LIKE THIS
  75.  
  76.  
  77. #!/BIN/SH
  78. OLDIFS=$IFS
  79. IFS='.'
  80. ECHO "PLEASE ENTER AN IP NO. : \C"
  81. READ PART1 PART2 PART3 PART4
  82. IFS=$OLDIFS
  83.  
  84. FOR PART IN $PART1 $PART2 $PART3 $PART4
  85. DO
  86.     RES=`EXPR $I + 0 2>/DEV/NULL`
  87.     [ "$RES" -LE 0 -O "$RES" -GT 255 ] && {
  88.            ECHO "ERROR IN I.P NUMBER "
  89.            EXIT 1
  90.     }
  91. DONE
  92.  
  93.  
  94. REGARDS
  95. STEVE WEET
  96.  
  97.  
  98.  
  99.  
  100.