I am working on a shell script which needs to know the logical name (eth0, eth1, etc) associated with a given ip. The current procedure I am employing is to parse the output of ifconfig using filters and getting the NIC card associated with a given IP. I was wondering if there exists a simpler way or a direct pipelined linux command to get the above mentioned detail?
Asked
Active
Viewed 667 times
1 Answers
4
Take this:
#!/bin/sh
ip=192.168.1.10
iface=$(ip addr | grep $ip | awk '{print $NF}')
echo "Iface is: ${iface}"

jgb
- 1,206
- 5
- 18
- 28
-
Brilliant... Thanks a lot. Can you please tell me the significance of $NF in the awk fliter ? That was one part I couldn't figure out. – csurfer Jul 08 '13 at 17:49
-
@csurfer Thanks for accepting this answer. `NF` is the number of fields in the current record. `$NF` points to the value of the last field. See `man awk`. you could also print: `$7`. – jgb Jul 08 '13 at 21:09
-
So $NF acts something like cut -d " " -f
– csurfer Jul 09 '13 at 19:09 -
NF is a variable pointing to the total number of fields. `cut` does not have a equivalent feature. Run: `echo 'one two three' | awk '{print NF,$NF}'` to see the difference between `NF` and `$NF` – jgb Jul 09 '13 at 19:37