Unix 简明教程

Unix / Linux - Shell Loop Control

在本章中,我们将讨论 Unix 中的 Shell 循环控制。迄今为止,您已了解如何创建循环并使用循环来完成不同任务。有时,您需要停止循环或跳过循环迭代。

In this chapter, we will discuss shell loop control in Unix. So far you have looked at creating loops and working with loops to accomplish different tasks. Sometimes you need to stop a loop or skip iterations of the loop.

在本章中,我们将学习用于控制 Shell 循环的以下两个语句 −

In this chapter, we will learn following two statements that are used to control shell loops−

  1. The break statement

  2. The continue statement

The infinite Loop

所有循环都有一个有限的生命周期,并且根据循环的不同,当条件为假或真时,它们就会退出。

All the loops have a limited life and they come out once the condition is false or true depending on the loop.

如果未满足所需条件,则循环可能无限期地继续。无限期执行并且不会终止的循环将无限次执行。因此,这样的循环称为无限循环。

A loop may continue forever if the required condition is not met. A loop that executes forever without terminating executes for an infinite number of times. For this reason, such loops are called infinite loops.

Example

下面是一个简单的示例,它使用 while 循环来显示数字 0 到 9 −

Here is a simple example that uses the while loop to display the numbers zero to nine −

#!/bin/sh

a=10

until [ $a -lt 10 ]
do
   echo $a
   a=`expr $a + 1`
done

此循环无限期地继续,因为 a 始终为 greater thanequal to 10 ,并且它永远不会小于 10。

This loop continues forever because a is always greater than or equal to 10 and it is never less than 10.

The break Statement

break 语句用于在执行到 break 语句的所有代码行后终止整个循环的执行。然后,它向下执行到循环结束后面的代码。

The break statement is used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement. It then steps down to the code following the end of the loop.

Syntax

以下 break 语句用于退出循环 −

The following break statement is used to come out of a loop −

break

break 命令还可以使用此格式从嵌套循环中退出 −

The break command can also be used to exit from a nested loop using this format −

break n

这里 n 指定了退出的 nth 循环。

Here n specifies the nth enclosing loop to the exit from.

Example

下面是一个简单的示例,它显示了当 a 变为 5 时循环如何终止 −

Here is a simple example which shows that loop terminates as soon as a becomes 5 −

#!/bin/sh

a=0

while [ $a -lt 10 ]
do
   echo $a
   if [ $a -eq 5 ]
   then
      break
   fi
   a=`expr $a + 1`
done

执行后,您会收到以下结果 −

Upon execution, you will receive the following result −

0
1
2
3
4
5

下面是嵌套 for 循环的一个简单示例。如果 var1 equals 2var2 equals 0

Here is a simple example of nested for loop. This script breaks out of both loops if var1 equals 2 and var2 equals 0

#!/bin/sh

for var1 in 1 2 3
do
   for var2 in 0 5
   do
      if [ $var1 -eq 2 -a $var2 -eq 0 ]
      then
         break 2
      else
         echo "$var1 $var2"
      fi
   done
done

执行后,您将收到以下结果。在内部循环中,您有一个 break 命令,参数为 2。这表示,如果满足条件,则您应该退出外部循环,并最终也退出内部循环。

Upon execution, you will receive the following result. In the inner loop, you have a break command with the argument 2. This indicates that if a condition is met you should break out of outer loop and ultimately from the inner loop as well.

1 0
1 5

The continue statement

continue 语句类似于 break 命令,只不过它导致退出循环的当前迭代,而不是整个循环。

The continue statement is similar to the break command, except that it causes the current iteration of the loop to exit, rather than the entire loop.

出现错误但您希望尝试执行循环的下一个迭代时,此语句很有用。

This statement is useful when an error has occurred but you want to try to execute the next iteration of the loop.

Syntax

continue

与 break 语句一样,可以向 continue 命令提供一个整数参数,以便从嵌套循环中跳过命令。

Like with the break statement, an integer argument can be given to the continue command to skip commands from nested loops.

continue n

这里 n 指定了继续执行的 nth 循环。

Here n specifies the nth enclosing loop to continue from.

Example

以下循环使用 continue 语句,它从 continue 语句返回并开始处理下一条语句 −

The following loop makes use of the continue statement which returns from the continue statement and starts processing the next statement −

#!/bin/sh

NUMS="1 2 3 4 5 6 7"

for NUM in $NUMS
do
   Q=`expr $NUM % 2`
   if [ $Q -eq 0 ]
   then
      echo "Number is an even number!!"
      continue
   fi
   echo "Found odd number"
done

执行后,您会收到以下结果 −

Upon execution, you will receive the following result −

Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number
Number is an even number!!
Found odd number