0

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

  • As a general aside, anything which looks like `grep | awk | tr | sed` is probably better off being refactored into a single Awk script. See also [useless use of `grep`](http://www.iki.fi/era/unix/award.html#grep) for the general idea. – tripleee Aug 25 '16 at 07:18
  • `ifconfig -a | awk '/Ethernet/ { printf("%s%s", s, $1); s=" " }'` – tripleee Aug 25 '16 at 07:19
  • Though if you want to end up with the values in an array, newlines are fine instead of single spaces, so just `awk '/Ethernet/ { print $1 }'` is fine for that. Maybe see also http://stackoverflow.com/questions/9084257/bash-array-with-spaces-in-elements – tripleee Aug 25 '16 at 13:10

1 Answers1

0

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.

Martin
  • 100
  • 2
  • 6
  • I'm sorry but this is not working even if i input only eth0. – Indigo Psy Aug 25 '16 at 07:05
  • oh, sorry, didnt catch logic of the script right away. Now i have eddited the comment, with doing array from the input of devices and from user input. Than switched logic of comparing with loops. Should be fine now. – Martin Aug 25 '16 at 07:12
  • Thank you very much Martin. This is working – Indigo Psy Aug 25 '16 at 07:15