linux 系統中while循環示例


1、linux系統中while循環示例

[root@linuxprobe test]# ls test.sh [root@linuxprobe test]# cat test.sh #!/bin/bash echo "the scale of price is 0-999!" ## 輸入內容提示 PRICE=`expr $RANDOM % 1000` ## 生成隨機的范圍變量,0-999 COUNTS=0 ## 定義初始次數為0 while true do read -p "please input your answer:" ANSWER let COUNTS++ ## 等價於 COUNTS=$[ $COUNTS + 1 ] if [ $ANSWER -eq $PRICE ] then echo "right,the price is $PRICE!" echo "you guess $COUNTS times!" exit 0 ## 等價於 exit / exit 1 / break elif [ $ANSWER -gt $PRICE ] then echo "high!" else echo "low!" fi done [root@linuxprobe test]# bash test.sh the scale of price is 0-999! please input your answer:500 low! please input your answer:700 low! please input your answer:800 high! please input your answer:750 high! please input your answer:730 low! please input your answer:740 high! please input your answer:735 right,the price is 735! you guess 7 times!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM