[QUESTION] While umatched error

Hi guys...was trying to use while loop and a sentry to prompt user input again if an invalid option was entered...but somehow I got a "while" unmatched error...The code is as below:

#!/system/bin/sh

finsh=0 
while [ $finish -ne 0 ] 
do 
echo "Please select an option: " 
echo "1. One" echo "2. Two" 
echo "3. Three" 
echo "4. Exit" 
echo -n "Enter your selection: "; read opt 

if [  $opt -eq 1 ]; then 
echo "Please enter your next selection: " 
echo "1. ABC"
echo "2. DEF" 
echo "3. Main Menu" 

subopt=0 
while [ $subopt -lt 1 ] || [ $subopt -gt 3 ] 
do 
echo -n "Enter your selection: "; read subopt 
if [ $subopt -eq 1 ]; then   
  echo "ABC"   
  echo "Hit enter to continue..."   
  read enterKey 
elif [ $subopt -eq 2 ]; then   
  echo "EFG" 
  echo "Hit enter to continue..." 
  read enterKey 
elif [ $subopt -eq 3 ]; then   
  echo "Returning to main menu" 
  echo "Hit enter to continue..." 
  read enterKey
else   
  echo "Invalid option, please try again..." 
fi 
done  

elif [ $opt -eq 2 ]; then 
echo "Two" 
echo "Press enter to continue..." 
read enterKey 

elif [ $opt -eq 3 ]; then 
echo "Three" 
echo "Press enter to continue..." 
read enterKey  

elif [ $opt -eq 4 ]; then 
echo "Bye!" 
finish=1  

else 
echo "Invalid option, please try again" 
fi 
done

exit

Thanks in advance...:slight_smile:

Hi, $finish does not get initialized prior to the while loop. One the line above that there is probably a typo, but even if it were set to zero, the while loop would not be entered..

1 Like

Ok...but even though I set the condition for the while loop as [ $finish -ne 1 ], I still get the same error...what should I do to make a correct initialisation?

You need to initialize the variable prior to the while loop test. The line above contains "finsh" instead of "finish"

1 Like

face palm silly me...what a typo...thanks!