Three methods for Shell to read files by row,
There are many methods for Shell to read files by row. The following three methods are written:
Statement 1:
#! /Bin/bash # Description: # while loop read Operations # Author: Standalone # version: 1.0 # Creation Time: 04: 08: 52 PATH =/bin:/sbin: /usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport PATHwhile read linedo echo $ linedone <filename (file to be read)
Statement 2:
#! /Bin/bash # Description: # cat Post-read Operations # Author: lone boat # version: 1.0 # Creation Time: 04: 08: 52 PATH =/bin:/sbin: /usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport PATHcat filename (file to be read) | while read linedo echo $ linedone
Statement 3:
#! /Bin/bash # Description: # for Loop read operations, which are different from while read Operations # Author: lone boat # version: 1.0 # Creation Time: 04: 08: 52 PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Binexport PATHfor line in 'cat filename (file to be read) 'do echo $ linedone
Note: for Loop read row operations, which are different from while read rows
While is completely read by line, no matter how many paragraphs in the line;
For is read by row. If the text in the row contains spaces, it is read separately, that is, one string is read at a time.