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 |
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"). |
Quote:
zsh Code:
ip="${$(ifconfig eth0)[7]/addr:}" Code:
set -- $(ifconfig eth0) P.S. This is for Linux, the other *nix flavours' ifconfig generates different output. |
Did anyone (else) have trouble with that bash snippet ???.
Failed on bash 3.2.21 - invalid substitution. Worked o.k. on 3.2.25 |
Quote:
(on my notebook with 3.2.25(1)). I'm trying to figure out which option could make it fail. |
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. |
Quote:
Code:
set -- $(LC_ALL=C ifconfig eth0) Thanks! |
Nope -same thing; error was "Syntax error: Bad substitution" BTW.
Running with "-x" didn't show any more info on the error. |
If you like regex, here's another way you could do it.
Code:
expr "$(ifconfig eth0)" : ".*inet addr:\([\.0-9]*\).*" |
Code:
> set -- $(/sbin/ifconfig eth0) |
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. |