The while command is a bit like a combination of if-then and for loops. The while test command returns a 0 exit state, looping through a set of commands.
While basic format:
While Test command
Do
Other commands
Done
Example:
#!/bin/bash
Varl=5
While [$varl-GT 0]
Do
Echo $varl
varl=$[$varl-1]
Done
[Email protected] ~]#./test10.sh
5
4
3
2
1
Note: Because while is detected when the exit status is 0, the loop is stopped only if the output is not 0.
varl=$[$varl-1] The result is not 0
Using multiple test commands
#!/bin/bash
Var1=3
While Echo $var 1
[$var 1-ge 0]
Do
echo "The Is inside the loop"
var1=$[$var 1-1]
Done
[Email protected] ~]#./test11.sh
3
The is inside the loop
2
The is inside the loop
1
The is inside the loop
0
The is inside the loop
-1
In multiple-line commands, all test commands are executed in each of the failed generations, including the last one to fail the test command
The while command in Linux