Topic: Test variable for ip

I need to check a variable in a shell script to see if it's an ip address. It feels like overkill to create a temporary file for it so I want to check against the variable. Also, it's not that important that it's a valid ip.

The script is in sh (bourne shell) and I'd appreciate all input. However, solutions in other languages i.e. bash or ksh are not of interest.

Thank you!

Re: Test variable for ip

I was having a brainlock.. to solve the problem I did like this:

echo "$current_ip" |grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"

Re: Test variable for ip

456.567.678.789 ?

Re: Test variable for ip

yeah, you might want to utilize hex/bin or check n<255.

Re: Test variable for ip

I don't get it man. First you want to check and then you don't care if it's a valid one. What do you mean?

Guru for a day, newbie for a lifetime

Re: Test variable for ip

I don't need to check if it's valid since the variable, in this specific case,  either is a valid ip address or a handful of error text. So by ruling out text, I sort of validated the results.

Thanks for your input.

Last edited by ibs (2006-09-17 03:08:37)