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
I would go like this:
devices="eth0 wlan0"
input="eth0 whlan0 wlan0"
#translate output strings to array based on space
IFS=' ' read -r -a devicesa <<< "$devices"
IFS=' ' read -r -a inputa <<< "$input"
for i in "${inputa[@]}"
do
for j in "${devicesa[@]}"; do
if [ ${i} == ${j} ]; then
correct=1
break
else
correct=0
fi
done
if [ $correct = 1 ]; then
echo "device $i is correct"
else
echo "device $i isnt correct"
fi
done
Maybe it could be more simplified, but you can read the steps to do. First go through array of strings, find devices, than compare them with user imput with writing walue about finding it. Last step is to clarify whether the value was found or not.