Read a file line
- For
- Command substitution
- Code block redirection
- While
- Pipe character
- Code block redirection
For
IFS=$‘\n‘forin-l`do (( count++ ))doneecho$count
Note:
For read, automatically press a space as an interval character. It is therefore necessary to define IFS to be delimited by line breaks
ls -l > forout.logmaxlength=$(wc -l < forout.log)forin`seq $maxlength`do (( count++ ))done < forout.log$count
Note:
The WC needs to use redirection, otherwise the output format is "number of rows file name."
While
whileread line do (( count++ )) donereadFile.log echo$count
Note
If the while condition expression does not write read, the line is output, but the result is the same
readwhileread line do #(( count++ )) echo"$line" done #echo $count
Note
The last output count is null, and it feels as if a pipe Fuzhou into a child process, and the child process variable cannot return to the parent process
Time
IME commands are used to print out a command or a program's execution time
The time command prints a standard error , in seconds, of how long a command takes to execute, the system time, and the time command.
Usage: Time [-P] Command [Argument ...]
The result of the time command consists of three rows: real, user, and Sys. We use real values here, and CPU time is divided into user and sys two blocks.
- The real value represents the elapsed time from the beginning of the program to the end of the program execution, including the CPU.
- The user value represents the program itself, and the time that the subroutines in the library it calls are used.
- SYS is the time that is executed by a system call that is called directly or indirectly by the program.
Test the four methods above
1.
Real 0m0.022s
User 0m0.000s
SYS 0m0.004s
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
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux shell reads rows of files line by line