LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-20-2005, 01:08 PM   #1
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
bash: ip address


Don't know if you can do this in the way that I presented. My script is pretty simple. It asks you for an ip address and it makes sure you entered the ip address syntactically correct.

#!/bin/bash

echo "What ip address would you like to enter?"
read ip
if [ $ip -ne [0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9].[0-9][0-9][0-9] ]; then
echo "Incorrect input. Must be in XXX.XXX.XXX.XXX. Exitting...."
exit 0
fi


As you can see, the user must type in something like 111.111.111.111. However, I get an error. How do you check for this in an if statement for bash? Can anyone help me? Thanks!

-twantrd
 
Old 04-20-2005, 02:14 PM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
From http://www.codecomments.com/Unix_She...ge453842.html:

Code:
#!/bin/bash

IP=$1

ONE=$(echo $1 | cut -f 1 -d '.')
TWO=$(echo $1 | cut -f 2 -d '.')
THREE=$(echo $1 | cut -f 3 -d '.')
FOUR=$(echo $1 | cut -f 4 -d '.')

if ! echo $IP | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'; then
   exit 10
fi

for i in $ONE $TWO $THREE $FOUR; do
   if [ ! $i -ge 0 -o ! $i -le 255 ]; then 
      echo "IP Address: $IP does not conform."
      exit 20
   fi
done

echo "IP Address: $IP conforms. Have a nice day."

Ivoke it like "./script 1.2.3.4".
You could just use
Code:
if ! echo $IP | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'; then
  echo "Yep!"
else 
  echo "Nope"
fi
If you wanted a really simple check. However, that simple regex won't bounds check, and octets of 0-999 will signal a "Yep!" printout.
 
Old 04-20-2005, 05:48 PM   #3
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Hey, thanks for the help rose! I actually got it figured out with 'case' instead. Thanks!

-twantrd
 
Old 04-21-2005, 08:40 AM   #4
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
w00t :-) Congrats!
 
  


Reply



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
dhcp no ip address and netmask dont match route address pengy666 Linux - Wireless Networking 1 05-08-2005 09:33 AM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM
Bash Script: Getting IP address fr0st Programming 3 01-14-2004 06:22 PM
bash script to detect scsi address at boot ewto Programming 2 10-20-2003 02:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:37 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration