while [options] [<condition>]
Display help.
The given condition is evaluated each time through the loop. If not specified the loop continues forever (or until a break statement). The conditional is an fshell expression that must return zero (KErrNone) in order for the loop to continue. It is common to use a var
command in the condition, although any command can be used if desired. Any further arguments or options will be coalesced into this one. [string]
Support for conditional loops in fshell scripts.
Execute all the following statements (up until endwhile
) repeatedly while condition
evaluates to KErrNone. The semantics are the same as for C-style while loops. For example:
export I 0 while var I lessthan 10 echo "Loop iteration $I" var I add 1 endwhile # $I is now 10
Indentation of the block is optional but recommended. There are a couple of restrictions on usage of while
and endwhile
that don't apply to most other fshell commands:
while and endwhile cannot participate in pipelines, nor can they be backgrounded using '&' or redirected. For example the following is not allowed: while 'var ERR == 0' && echo 'no error'
while and endwhile must each appear on a line on their own (although comments are permitted on those lines).
The endwhile
command will return an error if the most recently-entered block is not a while block.
Every while
command must be followed by exactly one endwhile
call, which cannot be in a different script. A missing endwhile will probably mean nothing from the while command onwards gets executed (although this behaviour may change).
Issuing the while command in an interactive fshell session is not a good idea and will probably do something strange. Currently, it will usually exit with an error or cause the shell to hang, requiring you to press CTRL-C to kill the while command.
Behaviour is unspecified if the above rules are ignored.
Nested while blocks are permitted providing there are the correct number of endwhile statements. Modifications to the environment within the while block (or even within the conditional, although this isn't recommended) are visible outside the block. In other words there is no scoping associated with the block.
Be careful when using dollar-expansion in the condition
argument - it will be expanded when the while command is constructed, ie before being used as the conditional, unless it is single-quoted. It is allowed to put multiple statements in the condition
, but note it must be quoted to prevent it appearing as if it is the while command that should be in the pipeline. Eg the correct way of including two conditions in the while statement is:
while 'var I == 0 && exists some_file' ... endwhile
Note: If you are executing a script using source and kill the script while it is exectuting a while
block (by for example pressing CTRL-C) it will not terminate the while block. This is not unique to the while command, no command that is in the middle of running in a source script will be terminated when pressing CTRL-C - it's just a bit more obvious when you're using the while command.
endwhile, break, continue, repeat, if
Copyright (c) 2011 Accenture. All rights reserved.