LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-09-2007, 02:52 PM   #1
Mangled
LQ Newbie
 
Registered: Jun 2007
Posts: 13

Rep: Reputation: 0
Cant store ip address to bash field


Hi All

Its probably something simple but no matter what I try the field always comes up blank, the problem is this, all I need is for a script to run and store the current ip address in a field for later use

I cant find much from google and the closest Iv I come to is on LinuxQuestions, but Im still new and dont understand a much about awk

ifconfig eth0 |grep "inet addr" |awk '{print $2}' |awk -F: '{print $2}'

Now how do I move the above output to a field, if you could direct me to a link or something would be great
 
Old 10-09-2007, 03:08 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
To store in a variable in the shell, use backticks (`), like so:

ip=`/sbin/ifconfig eth1 | grep "inet addr" | awk '{print $2}' |awk -F : '{print $2}'`

Edit: In this example, I've called the variable "ip". Of course, you're free to choose your own name! Then, when you want to get the value stored in that variable, you prefix the name with a $ (so, to print it for example, you'd use "echo $ip").

Last edited by Nylex; 10-09-2007 at 03:12 PM.
 
Old 10-09-2007, 03:29 PM   #3
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
ifconfig eth0 |grep "inet addr" |awk '{print $2}' |awk -F: '{print $2}'
You don't need all that pipes and commands:

zsh

Code:
ip="${$(ifconfig eth0)[7]/addr:}"
bash

Code:
set -- $(ifconfig eth0)
ip="${7/addr:}"

P.S. This is for Linux, the other *nix flavours'
ifconfig generates different output.

Last edited by radoulov; 10-09-2007 at 03:30 PM.
 
Old 10-09-2007, 09:04 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Did anyone (else) have trouble with that bash snippet ???.
Failed on bash 3.2.21 - invalid substitution. Worked o.k. on 3.2.25
 
Old 10-10-2007, 02:27 AM   #5
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by syg00 View Post
Did anyone (else) have trouble with that bash snippet ???.
Failed on bash 3.2.21 - invalid substitution. Worked o.k. on 3.2.25
Strange, worked with 2.05b.0(1), 3.2.9(1), 3.00.0(2)-bashdiff-1.44
(on my notebook with 3.2.25(1)).
I'm trying to figure out which option could make it fail.

Last edited by radoulov; 10-10-2007 at 02:31 AM.
 
Old 10-10-2007, 04:21 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Mmmm - very odd. Only fails on Ubuntu systems (so far). One 7.04 (bash 3.2.21) and one 7.10 beta (bash 3.2.25).
Arch and Gentoo work fine - I'll see what I can suss out.
 
Old 10-10-2007, 04:40 AM   #7
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by syg00 View Post
Mmmm - very odd. Only fails on Ubuntu systems (so far). One 7.04 (bash 3.2.21) and one 7.10 beta (bash 3.2.25).
Arch and Gentoo work fine - I'll see what I can suss out.
Could you try it like this:
Code:
set -- $(LC_ALL=C ifconfig eth0)
printf "%s\n" "${7/addr:}"

Thanks!
 
Old 10-10-2007, 05:21 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,103

Rep: Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117Reputation: 4117
Nope -same thing; error was "Syntax error: Bad substitution" BTW.
Running with "-x" didn't show any more info on the error.
 
Old 10-10-2007, 05:31 AM   #9
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
If you like regex, here's another way you could do it.
Code:
expr "$(ifconfig eth0)" : ".*inet addr:\([\.0-9]*\).*"
or
ifconfig eth0 |grep -o 'inet addr:[\.0-9]*'|cut -d: -f2
Not pretty, but it works.

Last edited by angrybanana; 10-10-2007 at 05:35 AM.
 
Old 10-10-2007, 05:34 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
> set -- $(/sbin/ifconfig eth0)
> ip=${7/addr:}
echo $ip
192.168.1.105
 
Old 10-10-2007, 01:32 PM   #11
Mangled
LQ Newbie
 
Registered: Jun 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks for the replys I wasn't expecting such a response from what I thought was a simple question, obviously theres a lot of variants as to the version your using, a lot of the posts are a bit over my head so I'm not going to comment on them, but thanks again
 
  


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
Kmail and auto complete of email address in the TO field shazam75 Linux - Software 3 08-14-2013 12:17 PM
Bash store last line from displayed text output in a variable carl0ski Programming 1 01-16-2007 03:38 AM
Find a field number for a value in text value - BASH severian23 Programming 3 01-10-2007 09:49 AM
Icon in address field Kanon Linux - Networking 2 03-22-2005 12:42 AM
where does the kde address book store its data? TheOneAndOnlySM Linux - Software 2 11-05-2004 05:59 PM

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

All times are GMT -5. The time now is 03:25 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