while
Execute consequent-commands as long as test-commands has an exit status of zero
Syntax
while test-commands; do consequent-commands; done
The return status is the exit status of the last command executed in consequent-commands, or zero if none were executed.
until
Execute consequent-commands as long as test-commands has an exit status which is not zero.
Syntax
until test-commands; do consequent-commands; done
The return status is the exit status of the last command executed in consequent-commands, or zero if none was executed.
for
Expand words, and execute commands once for each member in the resultant list, with name bound to the current member.
Syntax
for name [in words ...]; do commands; done
for (( expr1 ; expr2 ; expr3 )) ; do commands ; done
If `in words' is not present, the for command executes the commands once for each positional parameter that is set, as if `in "$@"' had been specified (see Positional Parameters below.)
The second form of the for command is evaluated thus:
First, the arithmetic expression expr1 is evaluated according to shell arithmetic expression rules. The arithmetic expression expr2 is then evaluated repeatedly until it evaluates to zero.
Each time expr2 evaluates to a non-zero value, commands are executed and the arithmetic expression expr3 is evaluated. If any expression is omitted, it behaves as if it evaluates to 1.
Return Status
The Return Status of for will be the exit status of the last command that executes, (if there are multiple expressions then the last command in list .)
If there are no items in the expansion of words, no commands are executed, and the return status is zero. The return status is false if any of the expressions is invalid.
Positional Parameters
These are assigned from the shell's arguments when the shell is invoked, they can be reassigned using the set builtin command.
Positional parameter N may be referenced as ${N}, or as $N when N consists of a single digit. $1, $2 etc
Examples
# Loop through a set of strings:
for m in Apple Sony Panasonic "Hewlett Packard" Nokia
do
echo "Manufacturer is:" $m
done
# or as a single line...
for m in Apple Sony Panasonic "Hewlett Packard" Nokia; do echo "Manufacturer is:" $m;done
# Loop 100 times:
for i in $(seq 1 100); do echo -n "Hello World${i} "; done
# Loop through the arguments passed to a function:
foo ()
{
for ARG in "$@";do echo $ARG; done
}
# try it
foo abc 123 "Hello World" 'bash rules'
ForEach (loop statement)
Loop through a set of input objects and perform an operation (execute a block of statements) against each .
Syntax
ForEach (item in collection) {ScriptBlock}
key
item A variable to hold the current item
collection A collection of objects e.g. filenames, registry keys, servernames
ScriptBlock A block of script to run against each object.
The collection will be evaluated and stored as an array in memory before the scriptblock is executed.
The foreach statement does not use pipelining (unlike ForEach-Object ) If you use foreach in a command pipeline PowerShell uses the foreach alias that calls the Foreach-Object.
Use the ForEach statement when the collection of objects is small enough that it can be loaded into memory.
Use the ForEach-Object cmdlet when you want to pass only one object at a time through the pipeline, minimising memory usage.
Examples
Loop through a collection of the numbers 1-5, echo each number unless the number is 2:
PS C:>foreach ($num in 1,2,3,4,5) { if ($num -eq 2) { continue } ; $num }
Loop through a collection of .txt files:
PS C:>foreach ($my_file in get-ChildItem *.txt) { if ($my_file.name -eq "dontwant.txt") { continue } ; $my_file.name }