Linuxshell reads the file line by line
Read File lines
Command to replace code block redirection while
Pipeline Code block redirection
IFS=$'\n'for line in `ls -l`do (( count++ ))doneecho $count
Note:
When reading for, the system automatically uses spaces as the delimiter. Therefore, you need to define IFS as separated by line breaks
ls -l > forout.logmaxlength=$(wc -l < forout.log)for i in `seq $maxlength`do (( count++ ))done < forout.logecho $count
Note:
Wc requires redirection. Otherwise, the output format is "number of rows file name ".
while
while read line do (( count++ )) done < readFile.log echo $count
Note:
If the while condition expression does not write read, line is output, but the final result is the same.
cat readFile.log | while read line do #(( count++ )) echo "$line" done #echo $count
Note:
The final output count is a null value. It feels that a pipeline is a sub-process, and the sub-process variables cannot return the parent process.
time
Ime commandPrint the execution time of a command or program
The time Command prints the time, system time, and time used to execute a command in seconds.Standard Error.
Usage: time [-p] Command [Argument... ]
The time Command result consists of three lines: real, user, and sys. Here we use real values, and the CPU is divided into user and sys.
The real value indicates the time consumed from the beginning of the program to the end of the program execution, including the CPU time. The user value indicates the time used by the program and the Child routines in the library it calls. Sys is the time when a system call is executed by a program directly or indirectly.
Test the above four methods
1.
Real 0m0. 022 s
User 0m0. 000 s
Sys 0m0. 004 s
2.
real 0m0.066s
user 0m0.004s
sys 0m0.016s
3.
real 0m0.001s
user 0m0.000s
sys 0m0.000s
4.
real 0m0.015s
user 0m0.000s
sys 0m0.008s