jeo wrote:
Hi SteveC, welcome!
Here's a neat function that I found on another forum that looks pretty clean, using awk:
Code:
if [ -n "$(echo $IP|egrep '([0-9]{1,3}\.){3}[0-9]{1,3}')" ]; then
echo "$IP" | awk -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
I hope this helps!
-J
Thanks, I'll try and mess with that because I am not too familiar with awk or |egrep and the format in the if statement that validates the numerical values. I'll take a look into some more guides so I can eventually filter different sets of user input. If you have any suggestions for tutorials I would greatly appreciate it.
Patsie wrote:
Code:
if [ "255" > "$1" ] && [ "$1" > "0" ] && [ "255" > "$2" ] && [ "255" > "$3" ] && [ "255" > "$4" ]
the > character is used to redirect output from a command to a file (or device)
If you want a 'less than' or 'greater than' operator, lose the quotes and use
-lt or
-gtI actually did use the -... operator at first, but when it didn't work I go rid of it and never tried it again when I revised the actual validation sequence. I knew that the > or >> could replace/send text to a file, but I guess my inexperience didn't think it would effect an if statement. Thanks a lot though that will help with lots of things.
As for anyone else that may happen to look at this thread; if you know of any good tutorials for more advanced coding (with explanations of what the given code does broken down) please leave a link on this post. I'm really sorry if there are guides on the forum I missed (I looked through the stickies for the most part), but I am very new and plan to stick around for a while to learn and hopefully one day help people with their issues.