尝试创建一个带有嵌套 while 循环的程序

尝试创建一个带有嵌套 while 循环的程序

代码摘要(或者应该做什么):用户收到一个问题,用户必须输入 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!"

相关内容