while循环

while循环

while condition; do

循环体

done

condition:循环控制条件:进入循环之前,先做一次判断;每一次循环之后会再次做判断;

条件为true,则执行一次循环;直到条件测试状态为false终止循环;

因此:condition中一般有循环控制变量;而此变量的值会在循环中不断的被修正;

实例:求100以内的正整数和;

#!/bin/bash

#

declare -i sum

declare -i i=1

while [ $i -le 100 ];do

let sum+=$i

let i++

done

echo "$sum

ping命令探测172.16.6.0-254在线的主机

#!/bin/bash

#

declare -i ip=1

declare -i up=0

declare -i down=0

while [ $ip -le 254  ];do

 ping -w 1 172.16.6.$ip &>/dev/null 

if [ $? -eq 0 ];then

echo "172.16.6.$ip is online"

let up++ 

else

echo "172.16.6.$ip is down" 

let down++

fi

let ip++

done

echo "online is $up"

echo "down is $down" 

打印九九乘法表

#!/bin/bash

#

for x in {1..9};do

for y in `seq 1 $x`;

do 

echo -e -n "$[x]X$[y]=$[$x*$y]\t"

done

echo

done

#!/bin/bash

#

declare -i x=1

declare -i y=1

while [ $x -le 9 ];do

while [ $y -le $x ];do

echo -e -n "$y*$x=$[$y*$x]\t"

let y++

done

let y=1

let x++

done

利用random生成10个随机数,输出这10个数,并显示其中最大的和最小的;

#!/bin/bash

#

declare -i max=0

declare -i min=0

declare -i i=1

rand=$RANDOM

echo $rand

max=$rand

min=$rand

while [ $i -le 9 ];do

rand=$RANDOM

echo $rand

if [ $rand -gt $max ];then

max=$rand

fi

if [ $rand -lt $min ];then

min=$rand

fi

let i++

done

echo "max=$max"

echo "min=$min"

#!/bin/bash

#

declare -i max=0

declare -i min=0

declare -i i=1

while [ $i -le 10 ];do

rand=$RANDOM

echo $rand

if [ $i -eq 1 ];then

   max=$rand

   min=$rand

fi


if [ $rand -gt $max ];then

max=$rand

fi


if [ $rand -lt $min ];then

min=$rand

fi

let i++

done

echo "max=$max"

echo "min=$min"



     本文转自阿伦艾弗森 51CTO博客,原文链接:http://blog.51cto.com/perper/1954274,如需转载请自行联系原作者





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值