
코드 요약(또는 수행해야 하는 작업): 사용자는 질문을 받고 5-20 사이의 유효한 참가자 번호를 입력해야 합니다. 그런 다음 이 입력 수에 따라 질문을 반복해야 합니다(참가자가 5명인 경우 이름 및 국가 질문을 5회 반복). 이름이 10자를 초과하거나 국가가 이탈리아가 아닌 경우 사용자에게 질문을 올바르게 반복하도록 요청하는 경고 메시지가 나타납니다. 신청자가 올바른 정보를 입력한 후 이름과 국가 입력을 모두 외부 파일로 보내야 합니다. 이는 각 신청자마다 반복되어야 합니다.
이름과 국가 값에 대한 루프를 구현하려고 시도했지만 무한히 루프하는 것처럼 보이거나 값 중 하나가 잘못된 경우 루프가 계속 진행됩니다. 나는 이것이 증분과 관련이 있다고 생각하지만 정확히 무엇을 해야할지 잘 모르겠습니다.
어떤 조언이라도 감사하겠습니다.
#!/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"
답변1
지난 주 게시물을 수정했습니다. 나는 당신이 Linux 스크립트에서 전송할 때 무슨 일이 일어났는지 믿습니다.아마도간격을 좀 어지럽혔어요. 모든 내용을 입력하겠습니다. 배울 수 있기를 바랍니다! 필요하다면 나에게 질문을 해주세요. 그리고 다시 한번 말씀드리지만, 터미널의 정확한 오류 코드와 같이 어떤 오류가 발생했는지 적어주세요!
#!/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!"