I want to validate did user input proper device in whiptail dialog or did user input something wrong.
I'm googling this for 2 days and can'f find any similar question/issue.
This is my code:
ALL_DEVICES=$(ifconfig -a | grep Ethernet | awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
U_INPUT=$(whiptail --title "[choose]" --inputbox "Please input your device" 0 0 all 3>&1 1>&2 2>&3)
If I do echo "$ALL_DEVICES" I'll get: eth0 wlan0
Let's assume that user input: eth wlan0 wlan1
How can I inform user that he input correctly: wlan0 , BUT eth and wlan1 is incorrect input, since that devices doesn't exist.
I tried this code:
ALL_DEVICES=$(ifconfig -a | grep Ethernet | awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
U_INPUT=$(whiptail --title "[choose]" --inputbox "Please input your device" 0 0 3>&1 1>&2 2>&3)
arr1=("$ALL_DEVICES")
arr2=("$U_INPUT")
echo "arr1 ${arr1[@]}"
echo "arr2 ${arr2[@]}"
FOUND="echo ${arr1[*]} | grep ${arr2[*]}"
if [ "${FOUND}" != "" ]; then
echo "Valid interfaces: ${arr2[*]}"
else
echo "Invalid interfaces: ${arr2[*]}"
fi
Thank you very much