Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

construct:


Click here to view code image
while expression
do
statements
done


In tcsh, use the following format:


Click here to view code image
while (expression)
statements
End


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
while [ $loopcount -lt 5 ]
do
loopcount=expr $loopcount + 1
increment=expr $loopcount \* 2
result=expr $result + $increment
doneecho "result is $result"


In tcsh, you can write this program as follows:


Click here to view code image
#!/bin/tcsh
set loopcount = 0
set result = 0
while ($loopcount < 5)
set loopcount = expr $loopcount + 1
set increment = expr $loopcount \* 2
set result = expr $result + $increment
end


echo    "result is  $result"

The until Statement


You can use the until statement to execute a series of commands until a
specified condition is true.


The loop terminates as soon as the specified condition evaluates to true.

Free download pdf