I am trying to see if a user entered input is contained in a space delimited list.
interfaces=`ls /sys/class/net | awk '{ ORS=" "; print; }'`
# Loop until valid input for interface is received
while [[ -z "$interface" || ! "$interfaces" =~ "$interface" ]]
do
echo -n "Select the interface ( "$interfaces"): "
read interface
done
$interfaces may contain something along the lines of "eth0 lo wlan0 wlan1 " and I am trying to see if the user has entered an interface that is in that list, if not tell them to do it again.
I can't seem to figure out how to do this. I tried with wildcards and the == operator as well as regex matching with =~ but I haven't had much luck.
Is there a simple and clean way of checking to see if the user inputted value is within the list/string created by me?
Thanks for any help!
$interfaces.lsprogramatically is never good practice.interfaces=( /sys/class/net/* ); interfaces=( "${interfaces[@]##*/}" )is a better way to get an array of interface names. (Mind you, being an actual array rather than a space-separated string, the process for expanding it is a bit different)interfaces=( /sys/class/net/* ); if [[ " ${interfaces[*]} " =~ " $foo " ]]; then ..., if you wanted to stick with string comparison for determining membership.