Linux shell reads the file line by line,
Read File lines
- For
- Command replacement
- Code block redirection
- While
- Pipeline operator
- Code block redirection
For
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. 066 s
User 0m0. 004 s
Sys 0m0. 016 s
3.
Real 0m0. 001 s
User 0m0. 000 s
Sys 0m0. 000 s
4.
Real 0m0. 015 s
User 0m0. 000 s
Sys 0m0. 008 s
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.