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!
复制代码

 

posted @   小鲨鱼2018  阅读(781)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示