Linux的 - bash脚本来计算时间已过 [英] Linux - bash script to calculate time elapsed

查看:141
本文介绍了Linux的 - bash脚本来计算时间已过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在bash编写脚本来计算经过我的命令的执行,考虑时间:

I am writing a script in bash to calculate the time elapsed for the execution of my commands, consider:

STARTTIME=$(date +%s)
#command block that takes time to complete...
#........
ENDTIME=$(date +%s)
echo "It takes $($ENDTIME - $STARTTIME) seconds to complete this task..."

我想我的逻辑是正确的,但是我结束了以下打印出来:

I guess my logic is correct however I end up with the following print out:

这需要几秒钟才能完成这个任务。

"It takes seconds to complete this task..."

什么不对我的字符串评估???

Anything wrong with my string evaluation???

我相信bash的变量是无类型的,如果有一个字符串到整数的方法在bash不过我喜欢。

I believe bash variables are untyped, I would love if there is a "string to integer" method in bash nevertheless.

推荐答案

$(()) $ [] 将用于计算的算术运算的结果的工作。您使用 $()这是简单地把字符串,并将其作为评价一个命令。这是一个有点微妙的区别的。希望这有助于。

Either $(()) or $[] will work for computing the result of an arithmetic operation. You're using $() which is simply taking the string and evaluating it as a command. It's a bit of a subtle distinction. Hope this helps.

随着天衣在这个答案的评论中指出, $ [] 是德preciated和 $(())应青睐。

As tink pointed out in the comments on this answer, $[] is depreciated, and $(()) should be favored.

这篇关于Linux的 - bash脚本来计算时间已过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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