Skip to content

循环语句

for

示例1

shell
for ((;;))
do
  read var
  if [ "$var" = "." ]; then # 直到用户输入.才跳出循环
    break
  fi
done

示例2

shell
#!/bin/bash

for number in 1 2 3 4 5 6
do
  echo "number is $number"
  if [ "$number" = "3" ]; then
    break
  fi
done

while

示例

shell
#!/bin/bash

number=0
while [ "$number" -lt 10 ]; do
  echo "Number = $number"
  number=$((number + 1))
done