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?
ifconfig wlan0.ifconfig | grep '^wlan' | head -1 | cut -d' ' -f1?