Syntax for While loop:
While condition test
Do
Command Area
Done
Example:
#!/bin/bashdeclare-i i=1declare-i Sum=0while ((i<=10)) Dolet Sum+=ilet ++idoneecho $sum
Use while to read file contents
While loop, the classic usage is to go with the switch input, read the contents of the file, as follows:
#!/bin/bashwhile Read Linedoecho $linedone < Cvsfile
- Line 3, use read to read the data from the standard input, put into the variable lines, if the read data is not empty, enter the loop.
- Line 6, the contents of the Cvsfile to the input, to read reading.
One more example:
#!/bin/bashifs= ': ' While read F1 F2 F3 f4 f5 f6 f7doecho ' account: $f 1,login Shell Yes: $f 7 "Done <"/etc/passwd "
Line 3, read/etc/passwd each line account data, and put the fields into the variable F1~F7.
While Infinite loop
Whlie ((1)) Doecho ' Sleepless ' done
It is also possible to change ((1)) to [1].
Or:
Whlie truedoecho ' Night sleepless ' done
Or:
Whlie:d oecho ' Night sleepless ' done
Type CTRL + C to terminate the loop.
Shell Getting Started Tutorial: Process Control (6) while loop