您的当前位置:首页正文

linux while循环

2024-11-20 来源:个人技术集锦
while循环的格式

1、计数器控制的while循环
  1. #!/bin/sh
  2. int=1
  3. while(( $int<=5 ))
  4. do
  5. echo $int
  6. let "int++"
  7. done
2、结束标记控制的while循环
  1.  #用脚本演示使用结束标记控制while循环实现猜1~10内的数
  2.  #!/bin/sh
  3.  echo "Please input the num (1~~10): "
  4.  read num
  5.  while [[ $num != 4 ]]
  6.  do
  7.  if [ $num -lt 4 ]
  8.  then
  9.  echo "Too small ,Try again.."
  10.  read num
  11.  elif [ $num -gt 4 ]
  12.  then
  13.  echo "Too big ,Try again.. "
  14.  read num
  15.  else
  16.  exit 0
  17.  fi
  18.  done
  19.  echo "Yes ,you are right !!"
3、标致控制的while循环
  1.  #!/bin/sh
  2.  echo "Please input the num:"
  3.  read num
  4.  sum=0
  5.  i=1
  6.  signal=0
  7.  while [[ $signal != 1 ]]
  8.  do
  9.  if [ $i -eq $num ]
  10.  then
  11.  let "signal=1"
  12.  let "sum+=i"
  13.  echo "1+2、、、+$num=$sum"
  14.  else
  15.  let "sum=sum+i"
  16.  let "i++"
  17.  fi
  18.  done
4、命令行参数控制的while循环
  1.  #!/bin/sh
  2.  echo "Please input arguements is $# "
  3.  echo "What you input : "
  4.  while [[ $* != "" ]]
  5.  do
  6.  echo $1
  7.  shift
  8.  done



显示全文