This article mainly introduces the output of the shell (bash) "time" command, and provides detailed sample code, I believe that it has some reference value for everyone's understanding and learning. if you need it, let's take a look at it. Preface
I believe everyone knows that time in bash is a very useful command. it can timing the execution of a script or a program, which is usually convenient when comparing the execution efficiency of the program roughly. However, you will find that the time text output by the time command cannot be simply redirected. for example, redirecting to a text file can only be displayed on the screen, which is inconvenient for non-interactive timing.
For example:
$ time find . -name "mysql.sh" >1.txt real 0m0.081suser 0m0.060ssys 0m0.020s $ time find . -name "mysql.sh" 2>2.txt./work186/sms/bin/mysql.sh./work186/sms/src/scripts/mysql.sh./work186/sms/src/scripts1/mysql.sh./work186/sms1/bin/mysql.sh./work186/sms1/src/scripts/mysql.sh./temp/sms/bin/mysql.sh./temp/sms/src/scripts/mysql.sh real 0m0.068suser 0m0.040ssys 0m0.030s
Through the above attempt, I found that I could not redirect the time output to the file. why? Because time is the shell keyword, shell performs special processing, it will process the command line following the time command as a whole. during redirection, in fact, the output of the time command itself is not redirected.
The keyword time sets a flag. Once the command (find) command is executed, timing information is printed to stderr. The time keyword requires the entire command and pipeline, and related redirection must be advanced. This is why simple redirection does not work for time. This is defined by Bash syntax. The redirection after command is part of the command for time.
Note: The time command is output to the standard error (stderr ).
When the time command is executed, the command runs in the next-level shell of the current shell (that is, the shell executed by the time command), and the output of time itself is located in the stderr of the current shell. As shown above, redirection only redirects the command stdout to a text file without outputting the time output.
The first solution is to place the time command and the command line to be executed in a shell code block, that is, a pair of braces. pay attention to the use of spaces and semicolons.
$ { time find . -name "mysql.sh"; } 2>2.txt./work186/sms/bin/mysql.sh./work186/sms/src/scripts/mysql.sh./work186/sms/src/scripts1/mysql.sh./work186/sms1/bin/mysql.sh./work186/sms1/src/scripts/mysql.sh./temp/sms/bin/mysql.sh./temp/sms/src/scripts/mysql.sh$ cat 2.txt real 0m0.068suser 0m0.030ssys 0m0.040s
The first method is successful. In summary, {time command-line;} 2> file must be used as the delimiter.
Another method is to use a sub-Shell.
As follows:
$ (time find . -name "mysql.sh") 2>2.txt./work186/sms/bin/mysql.sh./work186/sms/src/scripts/mysql.sh./work186/sms/src/scripts1/mysql.sh./work186/sms1/bin/mysql.sh./work186/sms1/src/scripts/mysql.sh./temp/sms/bin/mysql.sh./temp/sms/src/scripts/mysql.sh$ cat 2.txt real 0m0.083suser 0m0.040ssys 0m0.020s[root@web186 root]#
The second method also succeeded. In summary, it is (time command-line) 2> file. here, time is closely related to parentheses (which is also acceptable. the command line does not end with a semicolon. Of course, it is best to use the first method. after all, starting a sub-shell requires more resources.
Summary
The above is all about this article. I hope this article will help you in your study or work. if you have any questions, please leave a message.
For more details about the output of the "time" command under shell (bash), please follow the PHP Chinese network!