0

I was trying to get a substring in a bash script however the way I did is not a good solution. I'm parsing the response of "ifconfig" command and trying to get the first network interface name.

result of ifconfig:

eth0      Link encap:Ethernet  HWaddr b8:27:eb:6d:a1:92  
      UP BROADCAST MULTICAST  MTU:1500  Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

lo        Link encap:Local Loopback  
      inet addr:127.0.0.1  Mask:255.0.0.0
      UP LOOPBACK RUNNING  MTU:65536  Metric:1
      RX packets:73 errors:0 dropped:0 overruns:0 frame:0
      TX packets:73 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0 
      RX bytes:7099 (6.9 KiB)  TX bytes:7099 (6.9 KiB)

wlan0     Link encap:UNSPEC  HWaddr **** 
      UP BROADCAST RUNNING PROMISC MULTICAST  MTU:1500  Metric:1
      RX packets:792698 errors:0 dropped:792552 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:219274179 (209.1 MiB)  TX bytes:0 (0.0 B)

wlan5     Link encap:Ethernet  HWaddr ****  
      inet addr:****  Bcast:****  Mask:****
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:48934 errors:0 dropped:3422 overruns:0 frame:0
      TX packets:21217 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:14458518 (13.7 MiB)  TX bytes:2692948 (2.5 MiB)

getting first interface name which is wlan0

conf=`ifconfig`

net=${conf:670:6}

I didn't understand but position sometimes changes this is why i can't use index 670. Wlan0 can be wlan1,wlan2 and so on... I can't specifically search for wlan0. Any suggestions?

6
  • 1
    You can specify the interface to ifconfig: ifconfig wlan0. Commented May 5, 2015 at 7:06
  • Does it need to be the first network interface name (as you said at the beginning of the question) or does it have to be a wlan, as the end of the question suggests? Commented May 5, 2015 at 7:06
  • First interface name, in this case it is wlan0 but it can change Commented May 5, 2015 at 7:08
  • 1
    How about ifconfig | grep '^wlan' | head -1 | cut -d' ' -f1 ? Commented May 5, 2015 at 7:12
  • @tivn This is what I was looking for. Thank you. I just have a question what is "cut -d' ' -f1" used for? Commented May 5, 2015 at 7:23

3 Answers 3

1

GNU awk

ifconfig | awk -vRS= '!/^(eth0|lo)/{print $1;exit}'

Skips etho and lo blocks and prints the first field of the next, then quits.

Sign up to request clarification or add additional context in comments.

3 Comments

I got this error; awk: line 1: syntax error at or near ^
@AhmetTanakol i edited the answer a couple of seconds after posting, did you use the original? try it again.
this works also fine. It is similar to tivn's solution. He directly gets wlan, in your case you first eliminate eth0 and lo, then you get the next string output.
1

At first I thought the question was to get all the wlans and answered this:

 $ ifconfig | egrep -A6 '^wlan[0-9]:'

but then it was pointed out that this was GNU, not BSD so I should have said

 $ ifconfig | egrep -A6 '^wlan[0-9]'

(no colons). Then it was clarified that only the first one was needed, so perhaps

$ ifconfig | head -n 6

is a better answer?

If there are not exactly 6 lines in any description, then this isn't a very good answer.

Another approach is this:

$ ifconfig eth0 || ifconfig lo0 || ifconfig wlan0 || ifconfig wlan1

and so on. The || means if the first part fails, try the second. You'll get error messages until you hit one that works.

Now here is something better!

$ ifconfig | head -n 1

will give you the first one. Take that line, cut out the first thing on the line, then pass that to ifconfig. This should work on Linux:

$ ifconfig | head -n 1 | awk '{print $1}' | xargs ifconfig

7 Comments

Question says Wlan0 can be wlan1,wlan2 and so on...
Can it go beyond 9? If so, adjust my answer to add a plus sign after the [0-9].
Thank you, but I got multiple network interfaces so i just need to get the first one only which might be wlan0, wlan1, wlan2 etc...In this case by the way it prints nothing.
Oh, I will remove the colons; I'm on a BSD machine here and saw them. I will edit the answer.
Okay I just found an expression that should work for you. It is a little crazy, using xargs and awk. It is at the end of the long answer. You might be able to simplify it a bit, but hopefully it is a start.
|
0

What about something like?

ifconfig | grep -v "^ " "eth0" "lo" | head -1 | cut -c1-6

Have not a Linux pc on which to test it, though.

Basically, i just extract all the lines which do have the name of a net interface, removing all the eth0 and lo stuff, then I get the first one and get only the chars I need

4 Comments

Giving error; grep: eth0: No such file or directory grep: lo: No such file or directory
Yes this will give an error as only the first arg to grep is a pattern to search, all subsequent ones are filenames to search in.
The grep part should be: egrep -v '^( |eth|lo)'.
Yep @JID and tivn, you are more than right. I could not test it and overlooked the grep parameters part :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.