The shell's for loop format is as follows:
For variable in list
Do
...
Done
A list is a series of values that each value is separated by a space, once per loop, and the next value in the list is assigned to the variable
The in list is optional, if you do not use it, the For loop uses the command-line positional parameters
Example
Numbers in the output list
For x in 1 2 3 4 5
Do
Echo $x
Done
Strings in the output list
For x in "This Is My Girl"
Do
Echo $x
Done
All files in the output directory
For FI in *
Do
Echo $fi
Done
The while loop is used to continuously execute a series of commands and to read data from the input file.
While CMD
Do
...
Done
Example
Loop output 0-5 Digits
count=0;
While [$count-lt 5]
Do
Echo $count
Count= ' expr $count + 1 '
Done
While the while can also be used to enter an indefinite number of keyboard information ctrl+d can end the loop
While read film
Do
echo "Your most favrite film is $film"
Done
The Until loop executes a series of commands until the condition is true. The Until loop and the while loop are just the opposite of the processing mode. The general while loop is better than the until loop, but at some point it is only rare that the until loop is more useful.
Example:
#!/bin/bash
A=0
Until [! $a-lt 10]
Do
Echo $a
A= ' expr $a + 1 '
Done
Jump out of the loop
In the loop, sometimes you need to force out of the loop when the loop end condition is not reached, like most programming languages, the shell also uses break and continue to jump out of the loop.
Break command
The break command allows you to jump out of all loops (all loops after the execution is terminated).
Count=0
While [$count-lt 10]
Do
If [$count-eq 5]
Then
echo "Find You ${count}"
Break
Fi
Count= ' expr $count + 1 '
Done
Continue command
The continue command is similar to the break command, with only a little difference, and it does not jump out of all loops and just jumps out of the current loop.
#!/bin/bash
Count=1
While [$count-lt 5]
Do
Count= ' expr $count + 1 '
If [$count-eq 3]
Then
echo "Find You $count"
Continue
Fi
Echo $count
Done
Shell programming for while until loops