天天看点

while循环

while循环格式

while expression

do 

 command

 ......

done

第一种:计数器控制的循环

counter=1

 let command to operate counter

例:

#!/bin/bash

a=1

while (( "$a"<=4 ))

  echo "$a"

  let "a++"

第二种:结束标记控制的循环

read variable 

while [[ "$variable != sentinel ]]

do

 read variable

#!/bin/bash

echo "input the num(1-10)"

read num

while [[ "$num !=4 ]]

  if [ "$num" -lt 4 ]

  then

    echo "Too smanll.Try again"

    read num

  elif [ "$num" -gt 4 ]

    echo "to hgh.Tyr again"

  else

    exit 0

  fi

echo "you are right"

第三种:标志控制的循环

signal=0

while (( signal != 1 ))

  ...

  if expression

    signal=1

echo "Please input the num"

while [[ "$signal" != 1 ]]

  if [ "$num" -lt 4 ] 

     echo "Too small,Tye again"

      read num

  else [ "$num" -gt 4 ] 

     echo "To high.Try again"

     read num

     signal=1

     echo "you are right"

 fi

第四种:命令行控制的循环

while [[ "$*" != " " ]]

  echo "$1"

  shift

echo "number of arguments is $#"

echo "what you input is:"

继续阅读