linux中while循环语句
1、测试1 求1-100的和
[root@centos7 test2]# cat test.sh
#!/bin/bash
sum=0
a=1
while [ $a -le 100 ]
do
let sum+=$a
let a++
done
echo "the sum of 1-100 is: $sum"
[root@centos7 test2]# bash test.sh
the sum of 1-100is: 5050
2、⽤户输⼊决定程序循环的次数
[root@centos7 test2]# cat test.sh
#!/bin/bash
read -p "please input your choise: " choise
while [ $choise != q ]
do
read -p "input an number: " number
tmp=$(expr $number % 2)while语句简单例子
if [ $tmp -eq 0 ]
then
echo "even!"
else
echo "odd"
fi
echo "choose to continue or quit. q: quit; other:continue!"
read -p "please input your choise: " choise
done
[root@centos7 test2]# bash test.sh
please input your choise: e
input an number: 8
even!
choose to continue or quit. q: quit; other:continue!
please input your choise: y
input an number: 7
odd
choose to continue or quit. q: quit; other:continue!
please input your choise: q
3、编写程序每隔10秒显⽰系统负载、内存信息
[root@centos7 test2]# cat test.sh
#!/bin/bash
while true
do
uptime
free -h
echo "____________________________________________________________________________________________________" sleep 10
done
4、结合read语句逐⾏读取数据
[root@centos7 test2]# cat test.sh
#!/bin/bash
seq 50 | while read i
do
tmp=$(expr $i % 5)
if [ $tmp -eq 0 ]
then
echo "$i"
fi
done
[root@centos7 test2]# bash test.sh 5
10
15
20
25
30
35
40
45
50
继续
[root@centos7 test2]#
48
25
14
63
32
[root@centos7 test2]# cat test.sh #!/bin/bash
while read i
do
a=$(echo $i | cut -d "" -f 1)
b=$(echo $i | cut -d "" -f 2)
c=$(expr $a \* $b)
echo "$c"
done
[root@centos7 test2]# bash test.sh 32
10
4
18
6
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论