date的使用
脚本如下:
#!/bin/bash
#file name:cost_time.sh
start=$(date +%s) #命令执行前的纪元时
$@; #以("$1""$2"...)的形式保存所有输入的命令行参数
end=$(date +%s) #命令结束时的纪元时
cost_time=$[ end - start ] #时间相减
echo "Time taken to execte commands is $cost_time seconds."
执行结果如下:

本文介绍了一种使用Bash脚本测量命令执行时间的方法。通过在命令执行前后调用date命令并转换为纪元时间,计算出命令的运行时间。此脚本适用于需要评估命令执行效率的场景。
3320

被折叠的 条评论
为什么被折叠?



