check if argument is an ip address in bash/sh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check if argument is an ip address in bash/sh
# 1  
Old 08-17-2007
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
# 2  
Old 08-17-2007
If you had searched the forum you would have found this post - IP address validation function
# 3  
Old 08-17-2007
Well, I saw vino's reply only now that I've written my own function Smilie
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 08-17-2007
thanks vino and robotronic
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash argument not expanding in script

I pass an argument to bash as run. The first command in green executes as expected, however the second in blue fails as the $run does not expand. I tried to escape the variable with \ thinking the quotes were making the literal translation and also "${run}" but both did not work to expand the... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Argument check

All, I'm writing an argument checking block and running into an error. I want to confirm that $1 is one of two values. Here is what I have: if ]; then echo -e "\nPlease check your first augument. You used \"$1\" which is not recognized. Please see usage:" usage ... (9 Replies)
Discussion started by: hburnswell
9 Replies

3. Shell Programming and Scripting

Need a script to check if an argument is valid shell variable

I need a script that should print 'yes' if the argument is a valid shell variable name else 'No' if it is not a valid shell variable. A valid one begins with an alphabet or percentage (%) character and is followed by zero or more alphanumberic or percentage (%) characters. For example: $... (6 Replies)
Discussion started by: pingiliarjun
6 Replies

4. Shell Programming and Scripting

Check for spaces in input argument!

Hi guys, I have created a csh script which allows user to pass input argument with the script like: cluster_on_lev3.csh -t <value> -p <value> Example: cluster_on_lev3.csh -t 2.3 -p 0.05 Now I want to create an error code where if user passes input argument without spaces , the code... (16 Replies)
Discussion started by: dixits
16 Replies

5. Shell Programming and Scripting

How to use perl to run bash with argument?

Hi All, I want to run a bash script using perl. But they are in the different dir. #! /usr/bin/perl -w use strict; my $root=`pwd`; chomp($root); my $cmd=".$root/testdir/ft_623.sh 3 4 5 6 7"; print $cmd; my @line=`$cmd`; foreach (@line){ print $_; } ft_623.sh (0 Replies)
Discussion started by: Damon sine
0 Replies

6. Shell Programming and Scripting

Using GET, passing argument to bash

Hi guys! So, I use GET ( Simple user agent using LWP library. ) on a remote text file that is then passed to bash and executed. However, I need to pass that bash script a single argument, and so far nothing that I have tried has worked, although I have learned quite a bit about input/output... (5 Replies)
Discussion started by: Rhije
5 Replies

7. UNIX for Dummies Questions & Answers

Bash script argument problem

I'm having problems with bash scripts. If a bash script is called with no arguments, I always get "PHIST=!" as the first argument (i.e. this is what $1 equals). Why? Where does this come from, and how can I fix it? Nothing in the bash man pages refer to this mysterious default argument. (2 Replies)
Discussion started by: sszd
2 Replies

8. UNIX for Dummies Questions & Answers

Check for Empty Command Argument

I have a script that when called can have 1 or 2 command arguments. If only 1 command argument is passed into the script how can I check that the second argument is null? I am working in Korn shell in a UNIX environment. Example of script call with 2 arguments: % statreport 0300 1430 ... (6 Replies)
Discussion started by: Nysif Steve
6 Replies

9. Shell Programming and Scripting

Check if argument passed is an integers

How do I check if the argument passed to a script is an integer? I am writting a script that will take to integers and want to be able to check before I go on. I am using bourne shell. Thanks in advance (13 Replies)
Discussion started by: elchalateco
13 Replies

10. UNIX for Dummies Questions & Answers

how to check if the argument contain wildcard (*,?) ?

In a script , i would like to check if the argument ( $1, $2 inside the script) contain wildcard (*,? etc). how do i do it? > script_name arg1 arg* $1 (arg1) does not contain wildcard, but $2 (arg* )contains wildcard. how can i tell in script? i need to do this is because : if arg1... (3 Replies)
Discussion started by: gusla
3 Replies
Login or Register to Ask a Question