将一个进程运行所花费的秒数保存在一个变量中 [英] Save in a variable the number of seconds a process took to run

查看:73
本文介绍了将一个进程运行所花费的秒数保存在一个变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在bash中运行一个进程并将一个运行所花费的秒数保存在一个env变量中. 我该怎么做?

I want to run a process in bash and save in an env variable the number of seconds it took to run. How would I do such a thing?

推荐答案

您是要将此代码放入脚本中,还是要在启动脚本的过程中使用它?

Are you wanting to put this code in your script, or do it from the process that starts the script?

对于后者,您可以使用时间"保留字,然后解析其返回值以获取脚本花费的时间.

For the latter, you can use the "time" reserved word and then parse what it returns to get how much time a script takes.

如果要从脚本中执行此操作,可以将变量SECONDS设置为零,此后每次引用该变量时,它将更新为经过的秒数.因此,您可以在脚本的开头放置"SECONDS = 0",只要需要经过的时间,它就会在SECONDS变量中.

If you want to do this from within a script you can set the variable SECONDS to zero, and each time thereafter that you reference that variable it will be updated to be the number of elapsed seconds. So, you can put "SECONDS=0" at the very start of your script, and whenever you need the elapsed time it will be in the SECONDS variable.

您也可以在命令行上使用$ SECONDS技巧,例如:

You can also use the $SECONDS trick on the command line as well, for example:

$ SECONDS=0; sleep 5 ; echo "that took approximately $SECONDS seconds"

时间保留字和SECONDS变量都记录在bash手册页中.

The time reserved word and the SECONDS variable are both documented in the bash man page.

这篇关于将一个进程运行所花费的秒数保存在一个变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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