Samit Paudel
Samit Paudel

Reputation: 181

How do I limit user input to certain number of strings?

echo "Leonel Messi - LM, Harry Kane - HK, Neymar Junior - NJ, Keiran Trippier - KT"
echo "Enter 3 player codes:"
read p1 p2 p3
players=($p1 $p2 $p3)
        echo Empty field
    if [[ ${p[@]} -gt 3 ]]
    then
        echo More than 3 argument
    fi

I've tried this but it does not seem to do anything. Bash newbie here. I was looking to restrict user input to only three inputs and show error in case more than 3 inputs are entered.

Upvotes: 0

Views: 154

Answers (1)

Paul Hodges
Paul Hodges

Reputation: 15273

Simplest:

while (( 3 != ${#p[@]} )) # while 3 is not the number of elements in array p
do read -a p -p "Enter three (3) player codes: " # read elements into array p
done

Embellish for pretty as you like.

Upvotes: 1

Related Questions