
代碼摘要(或應該做什麼):使用者收到一個問題,使用者必須輸入 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!"