Method 1:while the most efficient and most commonly used method in the loop.
While read line
Do
Echo $line
Done < filename
Note: This is done at the end of the file, as if it were done and then read the file in the same way.
Method 2: Pipe method: Cat $FILENAME | While Read line
cat filename | while the read line
Do
Echo $line
Done
Note: When a pipeline is encountered, the output of the command on the left side of the pipeline is entered as input to the command on the right side of the pipeline.
Method 3 for Loop.
For line in ' cat filename '
Do
Echo ${line}
Done
Note: This is a way to read the contents of a file through a for loop rather than everyone is familiar with, here not much to say.
In each method, the For statement is the most efficient and the first is the most efficient when reading and writing files in a while loop.
For row-by-line reading and while progressive reading are different, such as:
$ cat T.txt
1111
2222
3333 4444 555
$ Cat T.txt | while read line; Do Echo ${line}; Done
1111
2222
3333 4444 555
$ for line in ' Cat t.txt '; Do Echo ${line}; Done
1111
2222
3333
4444
555
3 ways for the shell to read a file by line