Книга: Fedora™ Unleashed, 2008 edition

The until Statement

The until Statement

The until statement can be used to execute a series of commands until a specified condition is true. The loop terminates as soon as the specified condition evaluates to True.

In bash, the following format is used:

until expression
do
 statements
done

As you can see, the format of the until statement is similar to that of the while statement, but the logic differs: In a while loop, you execute until an expression iterates to False, but in an until loop, you loop until the expression iterates to True.

If you want to add the first five even numbers, you can use the following shell program in bash:

#!/bin/bash
loopcount=0
result=0
until [ $loopcount -ge 5 ]
do
 loopcount=`expr $loopcount + 1`
 increment=`expr $loopcount * 2`
 result=`expr $result + $increment`
done
echo "result is $result"

The example here is identical to the example for the while statement, except that the condition being tested is just the opposite of the condition specified in the while statement.

Оглавление книги


Генерация: 1.343. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз