shell/bash将time命令的结果输出到文件中 | 您所在的位置:网站首页 › shell执行脚本打印日志命令 › shell/bash将time命令的结果输出到文件中 |
shell/bash将time命令的结果输出到文件中
1. 目标2. 解决方案
1. 目标
在Linux中,使用time命令可以统计一个脚本的执行时间,比如time pwd就会打印出执行pwd脚本花费的时间(见下图)。但是,当我们想将time + 目标脚本的具体执行结果输出到文件中时,使用[shell] > output.file确无法达成目标(如下图,可以看到out1.txt和out2.txt中并没有real/user/sys的值,但是显示在屏幕上了)。 具体方案如下: # 1. 使用()将shell/bash脚本括起来 (time pwd) >& out1.txt # method 1 (time pwd) 2>&1 | tee out2.txt # method 2 (time pwd) > out3.txt 2>&1 # method 3 # 2. 使用{ ; }将shell/bash脚本括起来 { time pwd ; } >& out4.txt # method 4 { time pwd ; } > out5.txt 2>&1 # method 5 { time pwd ; } 2>&1 | tee out6.txt # method 6 # 3. 使用 /usr/bin/time指令而不是time指令,可以使用man time来查看/usr/bin/time的使用方法 /usr/bin/time pwd >& out7.txt /usr/bin/time pwd > out8.txt 2>&1 /usr/bin/time pwd 2>&1 | tee out9.txt /usr/bin/time -a -o out10.txt pwd > out10.txt注意事项:method 4~6中{}一定要有;而且;前后一定要有空格,否则会报错 具体执行结果如下: 相关参考: 输出 time 命令的结果到文件中 |
CopyRight 2018-2019 实验室设备网 版权所有 |