如何在 shell 脚本中以秒为单位测量持续时间? [英] How do I measure duration in seconds in a shell script?

查看:53
本文介绍了如何在 shell 脚本中以秒为单位测量持续时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一个操作在 Linux shell 脚本中需要多长时间.我该怎么做?

I wish to find out how long an operation takes in a Linux shell script. How can I do this?

推荐答案

正如其他人所建议的那样,使用 time 命令是个好主意.

Using the time command, as others have suggested, is a good idea.

另一种选择是使用魔法内置变量 $SECONDS,它包含自脚本开始执行以来的秒数.你可以说:

Another option is to use the magic built-in variable $SECONDS, which contains the number of seconds since the script started executing. You can say:

START_TIME=$SECONDS
dosomething
ELAPSED_TIME=$(($SECONDS - $START_TIME))

我认为这是特定于 bash 的,但由于您使用的是 Linux,我假设您使用的是 bash.

I think this is bash-specific, but since you're on Linux, I assume you're using bash.

这篇关于如何在 shell 脚本中以秒为单位测量持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆