2015年4月14日 发表评论 阅读评论

同事在写一个脚本的时候,在将time获取的执行时间结果输出到文件时,发现无法将结果输入到文件。通过测试发现可以通过如下方法实现time 结果输入到文件:

{ time sleep 1 ; } 2> time.txt
{ time sleep 1 2> sleep.stderr ; } 2> time.txt
(time ls) > outfile 2>&1
(time ls) > ls_results 2> time_results
/usr/bin/time -o time.txt sleep 1

Note: some shells (e.g., bash(1)) have a built-in time command that provides less functionality than the command described here. To access the real command, you may need to specify its pathname (something like /usr/bin/time).


man time的gun选项(即/usr/bin/time)发现有如下参数

-p     When in the POSIX locale, use the precise traditional format
       "real %f\nuser %f\nsys %f\n"
-o FILE, --output=FILE
         Do not send the results to stderr, but overwrite the specified file.


[root@361way ~]# /usr/bin/time -o time.txt sleep 1
[root@361way ~]# more time.txt
0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 2368maxresident)k
0inputs+0outputs (0major+182minor)pagefaults 0swaps


[root@361way ~]# /usr/bin/time -o time.txt -p sleep 1
[root@361way ~]# cat time.txt
real 1.00
user 0.00
sys 0.00


You can donate through PayPal.
My paypal id: itybku@139.com
Paypal page: https://www.paypal.me/361way

分类: shell脚本学习 标签: ,
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.