Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

In pdksh and bash, the following format is used:


Click here to view code image
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 is different: In a while loop, you execute
until an expression is false, whereas in an until loop, you loop until the
expression is true. An important part of this difference is that while is
executed zero or more times (so it is potentially not executed at all), but
until is repeated one or more times, meaning it is executed at least once.


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


Click here to view code image
#!/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.


The tcsh shell does not support the until statement.


The repeat Statement (tcsh)


You use the repeat statement to execute only one command a fixed number
of times.


If you want to print a hyphen (-) 80 times with one hyphen per line on the
screen, you can use the following command:


Click here to view code image
repeat 80 echo '-'

Free download pdf