agangliu0400 发表于 2018-5-22 06:06:03

linux while循环

  while循环的格式

[*]  while expression
[*]  do
[*]  command
[*]  command
[*]  ```
[*]  done
  

  1、计数器控制的while循环
  

[*]  #!/bin/sh
[*]  int=1
[*]  while(( $int<=5 ))
[*]  do
[*]  echo $int
[*]  let "int++"
[*]  done
  2、结束标记控制的while循环

  

[*]   #用脚本演示使用结束标记控制while循环实现猜1~10内的数
[*]   #!/bin/sh
[*]   echo "Please input the num (1~~10): "
[*]   read num
[*]   while [[ $num != 4 ]]
[*]   do
[*]   if [ $num -lt 4 ]
[*]   then
[*]   echo "Too small ,Try again.."
[*]   read num
[*]   elif [ $num -gt 4 ]
[*]   then
[*]   echo "Too big ,Try again.. "
[*]   read num
[*]   else
[*]   exit 0
[*]   fi
[*]   done
[*]   echo "Yes ,you are right !!"
  3、标致控制的while循环

  

[*]   #!/bin/sh
[*]   echo "Please input the num:"
[*]   read num
[*]   sum=0
[*]   i=1
[*]   signal=0
[*]   while [[ $signal != 1 ]]
[*]   do
[*]   if [ $i -eq $num ]
[*]   then
[*]   let "signal=1"
[*]   let "sum+=i"
[*]   echo "1+2、、、+$num=$sum"
[*]   else
[*]   let "sum=sum+i"
[*]   let "i++"
[*]   fi
[*]   done
  4、命令行参数控制的while循环

  

[*]   #!/bin/sh
[*]   echo "Please input arguements is $# "
[*]   echo "What you input : "
[*]   while [[ $* != "" ]]
[*]   do
[*]   echo $1
[*]   shift
[*]   done
  


[*]  

  
页: [1]
查看完整版本: linux while循环