I have a bug in my while loop check. I am checking if the user input is a valid user interface. This is my code:
#!/bin/bash
net_array=()
for iface in $(ifconfig | cut -d ' ' -f1| tr ':' '\n' | awk NF)
do
net_array+=("$iface")
done
unset "net_array[${#net_array[@]}-1]"
# Network Interface selection
printf "\nPlease select the network interface you want to use:\n"
read -r user_iface
while ! [[ "${net_array[@]}" =~ $user_iface ]]; do # check if the user input is valid
echo "Please enter a valid network interface:"
read -r user_iface
done
Generally this code works and checks if the element is in the array. My PC has eno1, eno2, eno3 interfaces and when I insert something different like eno5 it asks me again to insert the network interface.
The problem is that if I insert only 1
, it accepts it as a valid network interface, which is not the case and I want to exclude it. I guess I can perform additional check excluding all numerical user inputs, but I am wondering what my error is?