1

I have some lines like [ 17.440000] eth0: XYZCOMPANY ABC4353 802.11 Wireless Controller

[ 17.540000] eth1: XYZCOMPANY ABC4353 802.11 Wireless Controller

[ 17.640000] eth2: XYZCOMPANY ABC4353 802.11 Wireless Controller

I need to get eth0, eth1, eth2 into a variable in shell script. As you can notice all the other lines like XYZCOMPANY ABC4353 remains constant.but interface name is keep changing. I need to get those interface names into a variable.

Thanks!

1
  • Thanks. Small doubt..what happens if [ 17.640000] is present only attimes? Because the solution given below always expect ethX to be the third field right? But the time optional one and it may be absent too.. Commented Jul 19, 2010 at 13:14

3 Answers 3

2

This will show eth and wlan addresses with any number of digits:

var=$(dmesg | grep -Eo '(eth|wlan)[[:digit:]]+') 

If you don't need wlan you can remove the alternate. I'm not sure if the eth driver uses hex, but you can change "digit" to "xdigit" if it does and you need to allow for more than 10 intefaces.

3
  • Firstly thanks! Is the variable a array ? I need to evaluate interfaces one by one like eth0 , eth1 etc., Commented Jul 19, 2010 at 13:59
  • I can deal with it like an array like this : for i in ${var[@]}; do echo "hi" done Commented Jul 19, 2010 at 14:08
  • @kumar: You can do it without an array: for i in $var; do echo "interface: $i"; done. To make it an array, add a set of parentheses: array=($(dmesg | grep -Eo '(eth|wlan)[[:digit:]]+')). Commented Jul 19, 2010 at 15:43
0

This solution should be working too:

var=$(dmesg | grep "eth[0-9]: [A-Za-z0-9 ]* 802.11 Wireless Controller" | awk '{ print substr($3,0,5) }')

0

That is to have something like "eth0: eth1: eth2" in your variable

var=`awk /./'{print $3}' lala ` echo $var 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.