ネストされた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!"

おすすめ記事