Objective
I'm sure you all know that bash down time is a very useful command that can be timed for execution of a script or program, which is usually handy when it comes to a rough comparison of program execution efficiency. But you will find that the time text output by The Times command cannot be simply redirected, such as redirecting to a text file and only on the screen, which is inconvenient for non-interactive timing.
For example:
$ time Find. -name "mysql.sh" >1.txt real
0m0.081s
user 0m0.060s
sys 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.068s
user 0m0.040s
sys 0m0.030s
Through the above attempt, found that the time output information can not be redirected to the file, why? Because time is the shell's keyword, the shell does a special deal, it processes the command line that follows the Times command as a whole, and when redirected, it is actually for the following commands, and the output of the time command itself is not redirected.
The keyword time sets a tag to know that the command(find)
timing information is printed to the stderr when the command is executed. The time keyword has to be advanced for the entire command and pipeline, as well as related redirects. This is why simple redirection does not work for time. This is the definition of bash syntax. The redirection after the command is part of the command for time.
Note:the output of the time command to the standard error (STDERR)
When the time command executes, the command runs in the next shell of the current shell (the shell that the time command executes), and the output of times itself is in the stderr of the current shell. Redirection, as shown above, will 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 place the time command and the command line that will be executed into a shell block of code, which is a pair of curly braces, paying 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.068s
user 0m0.030s
sys 0m0.040s
The first approach to success, summed up is to { time command-line; } 2>file
note the use of delimiters.
The other way is to use the 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.083s
user 0m0.040s
sys 0m0.020s
[root@web186 root]#
The second way of trying is also successful, summed up here time is close to the (time command-line) 2>file
parentheses (also OK, the command line ends without having to take a semicolon.) Of course, it is best to use the first way, after all, start a child shell is to occupy more resources.
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.