Objective
I'm sure everyone knows bash. Time is a useful command that can be timed for a script or a program's execution, which is usually handy when you are roughly comparing the execution efficiency of a program. However, you will find that the time text of the command output cannot be simply redirected, such as redirecting to a text file that 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/s ms/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, it was found that the output information of time could not be redirected to the file, why? Because time is the shell's keyword, the shell makes a special deal, and it handles the command line as a whole at the back of the command, which, when redirected, is actually directed at the subsequent command, and the output of the command itself is not redirected.
The keyword time sets a flag that timing information is printed to stderr when command (find) is executed. The time keyword needs to be advanced for the entire command and pipeline, as well as for related redirects. That's why simple redirects don't work for time. This is the bash syntax definition. The redirect after command is part of the command for time.
Note: The output of the time command is in standard error (STDERR)
When the time command executes, the command runs at the next shell of the current shell (that is, the shell executed by the time command), and the output of time itself is in the stderr of the current shell. Redirects like the one shown above only cause the command's stdout to be redirected to a text file without outputting the output of time itself.
The first solution is to put the time command and the command line that will be executed in a shell code block, which is a pair of curly braces, and 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 attempt succeeded, summing up is {time command-line;} 2>file note the use of separators.
Another way is to use a child shell
As shown below:
$ (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 way of trying is also successful, summed up is (time command-line) 2>file here time close to the parentheses (also can, command line end does not have to take a semicolon.) Of course, it is best to use the first method, after all, to start a child shell is more resources.
Summarize
The above is the entire content of this article, I hope that the content of this article on everyone's study or work can bring certain help, if there is doubt you can message exchange.