Summary of code (or what it should do): User receives a question, user has to input a valid participant number between 5-20. Then, questions should be repeated based off the number of this input (if 5 participants, then repeat name and country questions 5 times). If the name is longer than 10 characters, or the country isn't Italy, then a warning message should appear to ask the user to repeat the question correctly. After the applicant has input the correct information, both name and country inputs should be sent to an external file. This should repeat for each applicant.
I've tried to implement a loop for the name and country values, but they seem to either loop infinitely, or when one of the values is incorrect, the loop keeps on going. I think this has something to do with the incrementation, but I'm not exactly sure what to do.
Any tips would be appreciated.
#!/bin/sh
i=0
read -p "Welcome to the lottery program. Please enter a number of participants between 5-20." input
while [ $input -lt 5 ] || [ $input -gt 20 ]
do
read -rp "Number of people must be 5-20" input
done
while [ $i -lt $input ]
do
read -p "Enter name(max 10 characters)" name
read -p "Enter country(only for people outside of Italy)" country
while [ ${#name} -gt 10 ]
do
read -p "The name was too long (over 10 chars). Please re-enter: " name
done
while [ "$country" = "Italy" ]
do
read -p "Italy is not included within the program. Please try again" country
done
while [ ${#name} -le 10 ] && [ "$country" != "Italy" ]
do
echo $name $country >>echo.txt
i=$((i+1))
done
done
echo "The records have been saved $input times"
${#name}
and$country
- so why are you testingwhile [ ${#name} -le 10 ] && [ $country != "Italy" ]
again? How are you expecting this loop to terminate (since nothing inside the loop affects the values)?