LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-22-2010, 06:27 AM   #1
shashv2
LQ Newbie
 
Registered: Jul 2010
Posts: 5

Rep: Reputation: 0
ip address of machine using bash script


I want to get only the IP address of the machine and store it in a variable. I have tried following commands and I get an error

IP =`ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
IP =$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')


Error is -

/filename.sh: line 2: IP: command not found

Any help will be appreciated.

Thanks,
 
Old 07-22-2010, 06:36 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
The spaces in "IP = ..." could be the problem. When you initialise variables in Bash, you must not use spaces.

Last edited by Nylex; 07-22-2010 at 06:58 AM.
 
Old 07-22-2010, 06:49 AM   #3
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
Or here is on other solution:
Code:
$ ifconfig eth0 |sed -n "/inet addr/p"| sed -e "s/\s*Bcast.*$//"|sed -e "s/^.*://"
192.168.123.102
 
Old 07-22-2010, 06:51 AM   #4
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
Sorry forget my previous post, I miss read the question, Nylex is correct.
 
Old 07-22-2010, 08:47 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Nylex has provided the correct answer to the assignment, but let us be concise with our commands. There is no need to use several when one is plenty:
Code:
#sed
ifconfig eth0 | sed -n -r '/inet addr/s/^[^:]*:([^ ]+) .*/\1/p'

#awk
ifconfig eth0 | awk '/inet addr/{print gensub(/addr:/,"","1",$2)}'
 
Old 07-22-2010, 10:04 AM   #6
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
I'll add some more hay to the stack. Altough this is not as short nor easy (and probably less efficient too) as the sed/awk solutions above, it'll demonstrate more ways to get around this problem, especially if one wishes to use something else than bash.

Code:
perl -e '$t=qx/ifconfig eth0/;if($t=~/\s*inet addr:([\d.]+)/){$ip=$1} print "$ip\n";'
Have a nice day

Last edited by b0uncer; 07-22-2010 at 10:04 AM. Reason: oops, it was eth0 in the examples, not wlan0..
 
Old 07-22-2010, 08:52 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ var=$(ifconfig wlan0)
$ var=${var##*inet addr:}
$ echo ${var%% *}
 
Old 07-22-2010, 09:00 PM   #8
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
OP's question has already been answered, but just for fun ... this works for any host on the LAN if you have nmbd running:

Code:
ip=$( nmblookup NETBIOSNAME | grep -v query | awk '{ print $1 }' )
 
Old 07-22-2010, 09:06 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Telengard View Post
OP's question has already been answered, but just for fun ... this works for any host on the LAN if you have nmbd running:

Code:
ip=$( nmblookup NETBIOSNAME | grep -v query | awk '{ print $1 }' )
grep and awk share the same functions.
Code:
ip=$( nmblookup NETBIOSNAME |awk '!/query/{ print $1 }' )
 
1 members found this post helpful.
Old 07-22-2010, 10:45 PM   #10
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by ghostdog74 View Post
Code:
ip=$( nmblookup NETBIOSNAME |awk '!/query/{ print $1 }' )
Nice simplification!
 
  


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
perl/bash script to monitor all processes running in my machine pudhiyavan Linux - Security 4 07-19-2005 02:09 PM
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 11:48 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