Ahmet Tanakol
Ahmet Tanakol

Reputation: 899

Getting a substring in bash script

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?

Upvotes: 0

Views: 149

Answers (3)

user4453924
user4453924

Reputation:

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.

Upvotes: 1

Ray Toal
Ray Toal

Reputation: 88378

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

Upvotes: 1

Davide
Davide

Reputation: 508

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

Upvotes: 0

Related Questions