day 9

while 循环

语法:

  while 条件;do ...;done

^C [root@iZwz96qzfgxh9l2rk7esxnZ shell]# vim while.sh

#!/bin/bash

while :

do  

load=`w | head -1 | awk -F 'load average:' '{print $2}' | cut -d . -f1 | sed 's/ //'`  

  if [ $load -gt 10 ]  

  then      

    echo $load  

  fi

done

~                     

 

 

 

 

 

 

 

 

 

^C [root@iZwz96qzfgxh9l2rk7esxnZ shell]# vim while2.sh

#!/bin/bash
while :
do
    read -p "Please input a number: " n
    if [ -z "$n" ]
    then
        echo "you need input sth."
        continue
    fi
    n1=`echo $n|sed 's/[0-9]//g'`
    if [ -n "$n1" ]
    then
        echo "you just only input numbers."
        continue
    fi
    break
done
echo $n

 

 

 

 

 

 

 

 

 

continue 重复该循环

break 跳出循环

[root@iZwz96qzfgxh9l2rk7esxnZ shell]# vim break.sh
[root@iZwz96qzfgxh9l2rk7esxnZ shell]# sh -x break.sh
++ seq 1 5
+ for i in '`seq 1 5`'
+ echo 1
1
+ '[' 1 -eq 3 ']'
+ for i in '`seq 1 5`'
+ echo 2
2
+ '[' 2 -eq 3 ']'
+ for i in '`seq 1 5`'
+ echo 3
3
+ '[' 3 -eq 3 ']'
+ break
+ echo ok
ok
[root@iZwz96qzfgxh9l2rk7esxnZ shell]#

 

posted @ 2018-12-26 23:41  依哈  阅读(132)  评论(0编辑  收藏  举报