shell/bash将time命令的结果输出到文件中 您所在的位置:网站首页 shell执行脚本打印日志命令 shell/bash将time命令的结果输出到文件中

shell/bash将time命令的结果输出到文件中

2023-08-11 05:02| 来源: 网络整理| 查看: 265

shell/bash将time命令的结果输出到文件中 1. 目标2. 解决方案

1. 目标

在Linux中,使用time命令可以统计一个脚本的执行时间,比如time pwd就会打印出执行pwd脚本花费的时间(见下图)。但是,当我们想将time + 目标脚本的具体执行结果输出到文件中时,使用[shell] > output.file确无法达成目标(如下图,可以看到out1.txt和out2.txt中并没有real/user/sys的值,但是显示在屏幕上了)。 time pwd脚本执行结果,其中”/home/yll/nksm/result“为pwd执行结果,real/user/sys为执行pwd脚本花费的时间

2. 解决方案

具体方案如下:

# 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中{}一定要有;而且;前后一定要有空格,否则会报错 具体执行结果如下: 在这里插入图片描述 命令执行后各个文件的内容如下:

绿框中为out16.txt中的内容,红框为out10.txt,out79.txt中的内容

相关参考:

输出 time 命令的结果到文件中



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有