![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to Validate | SanjayLinux | Shell Programming and Scripting | 10 | 09-27-2007 08:16 AM |
| validate each field in txt | happyv | Shell Programming and Scripting | 9 | 07-27-2007 06:24 AM |
| validate against a file | chiru_h | Shell Programming and Scripting | 7 | 08-31-2006 08:15 PM |
| validate the file name | maykap100 | Shell Programming and Scripting | 2 | 08-30-2005 11:30 AM |
| validate | ruffenator | Shell Programming and Scripting | 3 | 01-22-2002 10:37 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
How to validate an IP address
hi
I need a simple script to do the following. I need to check the user input for 1. quad dotted notation. 2. check that all of the inputs are numeric and within 1-255 range. Could someone please help me? I tried to use egrep to check for dotted notation. quad=1.1.1.12 QUAD4=`echo "${quad}" | egrep '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+'` echo $QUAD4 this validates ok excepts when I have a test ip address as "1.2.2.1a" it fails to validate if and return me the ip adrees itself. For the second step I have no clue . I would really appreciate if anyone could give any suggestion. Thanks, Sabina |
|
|||||
|
No problem - this should fix it....
Code:
#!/bin/sh
echo "Enter IP address"
read quad
oldIFS=$IFS
IFS=.
set -- $quad
if [ "$#" -ne "4" ]; then
echo "Must have 4 parts"
exit 1
fi
for oct in $1 $2 $3 $4; do
echo $oct | egrep "^[0-9]+$" >/dev/null 2>&1
if [ "$?" -ne "0" ]; then
echo "$oct: Not numeric"
exit 1
else
if [ "$oct" -lt "0" -o "$oct" -gt "255" ]; then
echo "$oct: Out of range"
exit 1
fi
fi
done
# New code from here....
echo "$quad" | grep "\.$" >/dev/null 2>&1
if [ "$?" -eq "0" ]; then
echo "Trailing period - invalid"
exit 1
fi
# to here.....
# if we're here, we're validated
echo "$quad validates OK!"
exit 0
ZB |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|