9.0;for, revision 9.0, 85/03/28
FOR -- Execute a FOR statement.
usage:
FOR var_name := int_expr [TO int_expr] [BY int_expr] command... ENDFOR
FOR var_name IN string_expr [BY {CHAR|WORD|LINE}] command... ENDFOR
FORMAT
FOR var_name := int_expr [TO int_expr] [BY int_expr] command... ENDFOR
FOR var_name IN string_expr [BY {CHAR|WORD|LINE}] command... ENDFOR
FOR allows you to build a control structure that executes commands repeatedly
as long as the result of a Boolean test is TRUE. The FOR command has two
formats: one for assigning and testing integer expressions, and one for
assigning and testing string expressions.
In the integer form, the (optional) TO and BY clauses permit you to specify
ranges and increment values, respectively. For example, you might want to
loop 5 times by saying
FOR a := 0 TO 10 BY 2
If you do not specify "BY int_expr", the default increment is 1. If you do
not specify "TO int_expr", you will probably want to increment the variable
manually inside the body of the loop. You should also put a test condition
inside the loop (and probably use an EXIT to get out) or else you risk looping
forever.
In the string form, the (optional) BY clause allows you to control the string
assignment operation. If you specify "BY WORD" (the default), each word (a
sequence of non-blank characters) in 'string_expr' is assigned to 'var_name'
one at a time until 'string_expr' is exhausted. You may also assign string
values a character at a time, or a line at a time, by using the "BY CHAR" and
"BY LINE" clauses, respectively.
ARGUMENTS
var_name
(required) Specify the name of the Shell variable whose value is to be
assigned and tested.
int_expr
(required) Specify any valid expression that returns an integer value.
string_expr
(required) Specify any valid expression that returns a string value.
command...
(required) Specify the command to be executed as long as the FOR test
returns TRUE. This may be a Shell command, a Shell script,
a variable assignment, or any other valid Shell operation.
Multiple commands are permitted; separate them with
semicolons or NEWLINE characters.
EXAMPLES
1. The following example demonstrates the advantages of a FOR loop
over a WHILE loop in one instance. Assume these line appear in a
Shell script.
#
# A loop using WHILE.
#
eon
a := 0
while ((^a <= 10)) do
args ^a
a := ^a + 2
enddo
#
# The same loop using FOR.
#
FOR a := 0 TO 10 BY 2
args ^a
ENDFOR
#
# End of script.
2. This example assigns three names to a variable.
#
# Script FILE_NAME
#
eon
FOR file IN "foo bar zap" BY word
args ^file
ENDFOR
#
# End of script.
Execution produces:
$ file_name
foo
bar
zap
$