Control structures operate on expressions involving vectors, constants, and ($-substituted) shell variables. A non-zero result (of any element, if the length is greater than 1) indicates ``true''. The following control structures are available:
Although control structures are most commonly used in command scripts, they are also allowed from the command line. While a block is active, the command prompt changes to one or more ``> '' characters, the number of which represents the current depth into the control commands. As with a UNIX shell, control structures can be used from the command line to repeat one or more commands.
repeat [number]Execute the statements in the block defined by the repeat line and the corresponding end statement number times, or indefinitely if no number is given. The number must be a constant, or a shell variable reference that evaluates to a constant, which may be a vector reference in the $& form. A vector name is not valid.
statement
...
end
while conditionThe while line, together with a matching end statement, defines a block of commands that are executed while the condition remains true. The condition is an expression which is considered true if it evaluates to a nonzero value, or if a vector, any component is nonzero. The test is performed at the top of the loop, so that if the condition is initially false, the statements are not executed.
statement
...
end
dowhile conditionThe dowhile line, together with a matching end statement, defines a block of commands that are executed while the condition remains true. The condition is an expression which is considered true if it evaluates to a nonzero value, or if a vector, any component is nonzero. Unlike the while statement, the test is performed at the bottom of the loop - so that the loop executes at least once.
statement
...
end
foreach var value ...The foreach statement opens a block which will be executed once for each value given. Each time through, the var will be set to successive values. After the loop is exited it will have the last value that was assigned to it. The var can be accessed in the loop with the $var notation, i.e., it should be treated as a shell variable, not a vector. This is set to each value as a text item.
statement
...
end
if conditionIf the condition is non-zero then the first set of statements is executed, otherwise the second set. The else and the second set of statements may be omitted.
statement
...
else
statement
...
end
label labelnameThis defines a label which can be used as an argument to a goto statememt.
goto labelIf there is a label statement defining the label in the block or an enclosing block, control is transfered there. If the goto is used outside of a block, the label must appear ahead of the goto (i.e., a forward goto may occur only within a block). There is a begin macro pre-defined as ``if 1'' which may be used if forward label references are required outside of a block construct.
continue [number]If there is a while, dowhile, foreach or repeat block enclosing this statement, the next iteration begins immediately and control passes to the top of the block. Otherwise an error results. If a number is given, that many surrounding blocks are continued. If there are not that many blocks, an error results.
break [number]If there is a while, dowhile, foreach, or repeat block enclosing this statement, control passes out of the block. Otherwise an error results. If a number is given, that many surrounding blocks are exited. If there are not that many blocks, an error results.
endThis statement terminates a block. It is an error for an end to appear without a matching if, while, dowhile, foreach, or repeat statement. The keywords endif, endwhile, enddowhile, endforeach, and endrepeat are internally aliased to end.
Control structures may be nested. When a block is entered and the input is from the keyboard, the prompt becomes a number of > 's equalling the depth of blocks the user has entered. The current control structures may be examined with the debugging command cdump.