home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!linac!att!ucbvax!CSGRAD.CS.VT.EDU!ramakris
- From: ramakris@CSGRAD.CS.VT.EDU (S.Ramakrishnan)
- Newsgroups: comp.unix.questions
- Subject: Distinguish between numeic & nonumeric char strings in shell ?
- Message-ID: <9209040703.AA06681@csgrad.cs.vt.edu>
- Date: 4 Sep 92 07:03:57 GMT
- Sender: daemon@ucbvax.BERKELEY.EDU
- Organization: VPI&SU Computer Science Department, Blacksburg, VA
- Lines: 39
-
-
- How does one distinguish between a number and a nonnumeric
- character using shelltools (sh/csh, awk, sed, expr, etc) ?
- I am trying to validate the IP address typed in by the user.
- I isolate the four components and check each one to see if lies
- within the legal range (0..255). In the event the user types, say,
- tom.jack.joe.jim, I want to be able to flag an error. However the
- IF statement, marked (+) in the fragment below, fails even if a 4
- component string such as "tom.jack.joe.jim" is entered.
- Any suggestions ?
-
- Encl:
- #!/bin/sh
- ...
- while test "$ip" = ""
- do
- echo " IP Address = "
- read ip
- if [ "$ip" != "" ]
- then
- cnt=`echo $ip | awk -F'.' '{ print NF }'`
- one=`echo $ip | awk -F'.' '{ print $1}'`
- two=`echo $ip | awk -F'.' '{ print $2}'`
- thr=`echo $ip | awk -F'.' '{ print $3}'`
- fou=`echo $ip | awk -F'.' '{ print $4}'`
- #(+)
- if [ $cnt -ne 4 -o $one -gt 255 -o $two -gt 255 -o $thr -gt 255 -o $fou -gt 255 ]
- then
- echo ""
- echo "*** Invalid IP Address Format: Please Retype"
- echo ""
- ip=""
- fi
- fi
- done
- ...
-
- ---
- S Ramakrishnan, CS Dept, McBryde Hall, VaTech, Virg.
-