Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

check if argument is an ip address in bash/sh

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-17-2007
marcpascual marcpascual is offline
Registered User
 
Join Date: Sep 2005
Last Activity: 2 January 2013, 2:54 AM EST
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
check if argument is an ip address in bash/sh

Hi all,

Can you please suggest a few lines of if statement to check if a variable is an ip address purely in bash/sh?

Thanks,
Marc
Sponsored Links
    #2  
Old Unix and Linux 08-17-2007
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 12 March 2015, 5:33 AM EDT
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 Posts
If you had searched the forum you would have found this post - IP address validation function
Sponsored Links
    #3  
Old Unix and Linux 08-17-2007
robotronic's Unix or Linux Image
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
 
Join Date: Apr 2002
Last Activity: 20 December 2009, 10:56 AM EST
Location: Italy
Posts: 370
Thanks: 0
Thanked 0 Times in 0 Posts
Well, I saw vino's reply only now that I've written my own function Linux
I post my code anyway:


Code:
IP="123.246.189.235"
TEST=`echo "${IP}." | /usr/xpg4/bin/grep -E "([0-9]{1,3}\.){4}"`

if [ "$TEST" ]
then
   echo "$IP" | nawk -F. '{
      if ( (($1>=0) && ($1<=255)) &&
           (($2>=0) && ($2<=255)) &&
           (($3>=0) && ($3<=255)) &&
           (($4>=0) && ($4<=255)) ) {
         print($0 " is a valid IP address.");
      } else {
         print($0 ": IP address out of range!");
      }
   }'
else
   echo "${IP} is not a valid IP address!"
fi

Is tested on Solaris, you may have to change the grep invocation according to your platform (grep, grep -E, egrep, ...).
    #4  
Old Unix and Linux 08-17-2007
marcpascual marcpascual is offline
Registered User
 
Join Date: Sep 2005
Last Activity: 2 January 2013, 2:54 AM EST
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
thanks vino and robotronic
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to use perl to run bash with argument? Damon sine Shell Programming and Scripting 0 07-12-2010 02:02 PM
Using GET, passing argument to bash Rhije Shell Programming and Scripting 5 09-26-2009 05:56 AM
Check for Empty Command Argument Nysif Steve UNIX for Dummies Questions & Answers 6 09-19-2007 03:59 PM
Check if argument passed is an integers elchalateco Shell Programming and Scripting 13 11-07-2002 08:23 AM
how to check if the argument contain wildcard (*,?) ? gusla UNIX for Dummies Questions & Answers 3 04-02-2002 05:45 AM



All times are GMT -4. The time now is 01:55 PM.