Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
    
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.. –  kumar Jul 19 '10 at 13:14
add comment

3 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
Firstly thanks! Is the variable a array ? I need to evaluate interfaces one by one like eth0 , eth1 etc., –  kumar Jul 19 '10 at 13:59
    
I can deal with it like an array like this : for i in ${var[@]}; do echo "hi" done –  kumar Jul 19 '10 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:]]+')). –  Dennis Williamson Jul 19 '10 at 15:43
add comment

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) }')

share|improve this answer
add comment

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

var=`awk /./'{print $3}' lala `
echo $var
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.