bashshellifconfig

How do I make a list from ifconfig -a?


Hello I'm trying to use ifconfig -a to list out all interfaces and then further make it into a menu where you can select which interface to use. Right now I have ifconfig -a | sed 's/[ \t].*//;/^$/d' | nl -n ln -w 6 This displays

1 eth0:

2 lo:

3 wlan0:

I want to be able to press 1 or 2 or 3 and then have it assign the option's corresponding interface name to a variable, lets say $networkname. So when echo "${networkname}" is run it only displays eth0 or wlan0 or lo


Solution

  • I assume that you are using Linux:

    #!/bin/bash
    
    cd /sys/class/net || exit 1
    
    select int in *; do break; done
    echo "$int"