0

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"    
    
3
  • 2
    You already checked ${#name} and $country - so why are you testing while [ ${#name} -le 10 ] && [ $country != "Italy" ] again? How are you expecting this loop to terminate (since nothing inside the loop affects the values)? May 24, 2021 at 0:17
  • ... also remember to double-quote variable expansions - see When is double-quoting necessary? May 24, 2021 at 0:21
  • @steeldriver I want to make the loop only increment once both input values have been correct.
    – TheUser
    May 25, 2021 at 0:01

1 Answer 1

0

Edited from your last weeks's post. I believe what happen was when u transfer from your linux script, maybe you messed up some spacing. imma type the whole thing out, hope you can learn ! feel free to ask me questions if needed. And reminder again , please write what error you are facing , like the exact error code from the terminal!

#!/bin/sh
 
i=0
        
echo -e "Please enter a number of participants between 5-20 :\c"
read 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
        
if [ ${#name} -le 10 ] && [ "$country" != "italy" ]
then
i=$((i+1))
echo $name $country >> echoed.txt
elif [ ${#name} -gt 10 ]
then
read -rp "your name is too long! re-enter!"
else 
echo "Italy isnt accpeted"
fi
done
echo "All $input records are saved!"
7
  • Hi, in the meantime I created a new line of code with while loops (I heard this program can only be written using these loops), but I'm running into issues with the country and name questions constantly being repeated, even if one of them is false. Could you suggest anything to fix this?
    – TheUser
    May 24, 2021 at 13:50
  • @TheUser no you can make use of if else also. i get what you are trying to do. you try and use my code, it works perfectly May 25, 2021 at 1:43
  • and you HAVE TO use IF else , because you only want to exit the condition if something failed. but your code in ur question exits and reenters everywhere remember this : "Codes read line by line, so after reading your while, it will run , and so on. " May 25, 2021 at 1:49
  • I've just tried your code, and it works until I input the incorrect country name (Italy). An error message does pop up, but along with it the "Enter name" message also pops up.
    – TheUser
    May 25, 2021 at 1:51
  • @TheUser oh , yeah , i make a new line to pop up saying italy isnt accepted and then it will prompt the user to re enter the info again. let say u choose 5 people , on entering the 5th participant, and country is italy, it will prompt the user to enter again as the user entered italy. but in the saved record, there are 5 all together. meaning no additional name is saved even if u input italy May 25, 2021 at 3:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .