LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 08-18-2004, 07:17 AM   #1
rooch84
Member
 
Registered: Aug 2004
Posts: 33

Rep: Reputation: 15
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
 
Old 08-18-2004, 07:49 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.

Last edited by druuna; 08-18-2004 at 08:03 AM.
 
Old 08-18-2004, 08:21 AM   #3
rooch84
Member
 
Registered: Aug 2004
Posts: 33

Original Poster
Rep: Reputation: 15
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
 
Old 08-18-2004, 08:23 AM   #4
rooch84
Member
 
Registered: Aug 2004
Posts: 33

Original Poster
Rep: Reputation: 15
Just spotted the mistake...Bloody infinate loops!
 
Old 08-18-2004, 08:37 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 08-18-2004, 08:44 AM   #6
rooch84
Member
 
Registered: Aug 2004
Posts: 33

Original Poster
Rep: Reputation: 15
That also works a treat. But I don't undertand the "$?" in the if statement
 
Old 08-18-2004, 09:14 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Obtain ip address and check for running process via Bash Script? xconspirisist Programming 10 09-12-2008 01:18 PM
shell script to check ftp communication yuva_mca Linux - General 2 12-01-2005 07:15 AM
Shell Script to check root user? kushalkoolwal Programming 4 09-22-2005 12:15 AM
Bulk image format conversions with a shell script? infidel Linux - Software 4 11-16-2003 07:20 PM
Shell-Script check if $xx is of type integer d-fens Programming 5 07-11-2003 08:38 AM


All times are GMT -5. The time now is 12:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration