Search code examples
linuxbashnetwork-interface

Bash - Display menu with network interfaces as options


I am a total bash newbie, and I need to make a script that has a menu where each of the options is one of the available network interfaces of the system. The user must choose one of the options.

So basically, I am looking for something like this (when executed).

Please, select a network interface:

1)eth0
2)eth1
3)lo 

And then the user can input 1, 2 or 3 to select one of those options.

Any help will be appreciated. Thanks a lot!


Solution

  • Try this:

    echo "Please, select a network interface:"
    cd /sys/class/net && select foo in *; do echo $foo selected; done
    

    Output:

    Please, select a network interface:
    1) eth0
    2) lo
    #? 
    

    See: help select and this example to see how to use select and case.