home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!ftpbox!mothost!schbbs!maccvm.corp.mot.com!W10075
- From: W10075@maccvm.corp.mot.com (Stephen Weet)
- Subject: Re: Distinguish between numeric & nonnumeric char strings in shell ?
- Organization: Motorola
- Date: 4 Sep 1992 10:40:12 CDT
- Message-ID: <1992Sep4.155850.11074@schbbs.mot.com>
- Sender: news@schbbs.mot.com (Net News)
- Nntp-Posting-Host: maccvm.corp.mot.com
- Lines: 88
-
- ------------------------- Original Article -------------------------
- Path:
- schbbs!mothost!ftpbox!uunet!wupost!sdd.hp.com!hplabs!ucbvax!CSGRAD.CS.VT.EDU!ram
- akris
- From: ramakris@CSGRAD.CS.VT.EDU (S.Ramakrishnan)
- Newsgroups: comp.unix.shell
- Subject: Distinguish between numeric & nonnumeric char strings in shell ?
- Message-ID: <9209040702.AA06629@csgrad.cs.vt.edu>
- Date: 4 Sep 92 07:02:38 GMT
- Sender: daemon@ucbvax.BERKELEY.EDU
- ------------------------- Original Article -------------------------
- Path:
- schbbs!mothost!ftpbox!uunet!wupost!sdd.hp.com!hplabs!ucbvax!CSGRAD.CS.VT.EDU!ram
- akris
- From: ramakris@CSGRAD.CS.VT.EDU (S.Ramakrishnan)
- Newsgroups: comp.unix.shell
- Subject: Distinguish between numeric & nonnumeric char strings in shell ?
- Message-ID: <9209040702.AA06629@csgrad.cs.vt.edu>
- Date: 4 Sep 92 07:02:38 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
- ...
-
- ---
- YOU MIGHT TRY SOMETHING LIKE THIS
-
-
- #!/BIN/SH
- OLDIFS=$IFS
- IFS='.'
- ECHO "PLEASE ENTER AN IP NO. : \C"
- READ PART1 PART2 PART3 PART4
- IFS=$OLDIFS
-
- FOR PART IN $PART1 $PART2 $PART3 $PART4
- DO
- RES=`EXPR $I + 0 2>/DEV/NULL`
- [ "$RES" -LE 0 -O "$RES" -GT 255 ] && {
- ECHO "ERROR IN I.P NUMBER "
- EXIT 1
- }
- DONE
-
-
- REGARDS
- STEVE WEET
-
-
-
-
-