Shell script ip address format check.
I have a script that receives an ip address from the user and then sets up a ifcfg-bond0 file. I want to check to user input, to make sure it is in ip address format.
I'm guessing I need to use a regular expression. But I have't implemented one before. It basically needs to check for 4 sets of numbers with three dots in between, it can check that each number is between 0 and 255, but that isn't as important. Thanks Chris |
This regexp will do what you want: 4 sets of numbers in the range 0 - 255, seperated by a dot.
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b Using egrep you can determine if the given ip is correct. Short example: $ echo "1.2.3.4" | egrep '<regexp>' 1.2.3.4 $ echo "256.2.3.4" | egrep '<regexp>' <empty = incorrect> How the regexp works: \b (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \. (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \b The \b at the beginning and end are wordboundries and needed to limit the range (greediness). The (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) part (4 sets of them) make sure that the range for each set is between 0 and 255. The \., three times, are for the dot seperating the 4 sets of numbers. Hope this gets you going again. |
Works a treat. I've set it out like this:
echo -n "Please enter the IP Address for this machine: " read ipaddr regex="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" cd /tmp touch check_file echo $ipaddr | egrep $regex | tee check_file until -s check_file do echo -n "Incorrect IP address, please try again: " input ipaddr echo $ipaddr | egrep $regex | tee check_file done If there is a better method the please let me know. Thanks Chris |
Just spotted the mistake...Bloody infinate loops!
|
You don't need (want) the check_file, this I/O is not necessary.
This can be done within the script: regex="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(2 5[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" echo -n "Please enter the IP Address for this machine: " read ipaddr CHECK=$(echo $ipaddr | egrep $regex) if [[ "$?" -eq 0 ]] then echo -n "Correct IP address" else echo -n "Incorrect IP address, please try again: " fi This way you check the exit code that echo $ipaddr | egrep $regex produces (0 = ok, 1 = not ok). Script needs some work, but the priciple is there. |
That also works a treat. But I don't undertand the "$?" in the if statement
|
This ($?) holds the exit status of the last command executed.
You are probably familiar with: $1, $2 etc. This is just one more of the same 'family' Take a look at the 'Special Parameters' section in man bash to see some more of these special parameters. |
All times are GMT -5. The time now is 07:42 PM. |