LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Cant store ip address to bash field (https://www.linuxquestions.org/questions/programming-9/cant-store-ip-address-to-bash-field-590610/)

Mangled 10-09-2007 02:52 PM

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

Nylex 10-09-2007 03:08 PM

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").

radoulov 10-09-2007 03:29 PM

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.

syg00 10-09-2007 09:04 PM

Did anyone (else) have trouble with that bash snippet ???.
Failed on bash 3.2.21 - invalid substitution. Worked o.k. on 3.2.25

radoulov 10-10-2007 02:27 AM

Quote:

Originally Posted by syg00 (Post 2919114)
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.

syg00 10-10-2007 04:21 AM

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.

radoulov 10-10-2007 04:40 AM

Quote:

Originally Posted by syg00 (Post 2919400)
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!

syg00 10-10-2007 05:21 AM

Nope -same thing; error was "Syntax error: Bad substitution" BTW.
Running with "-x" didn't show any more info on the error.

angrybanana 10-10-2007 05:31 AM

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.

jschiwal 10-10-2007 05:34 AM

Code:

> set -- $(/sbin/ifconfig eth0)
> ip=${7/addr:}
echo $ip
192.168.1.105


Mangled 10-10-2007 01:32 PM

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


All times are GMT -5. The time now is 10:21 PM.