linux - 如何在 shell 脚本中将时间戳转换为小时/分钟/秒?

标签 linux bash shell date

我正在计算我的代码在 shell 脚本中花费了多少时间 -

date1=$(date +"%s")

# some code here

date2=$(date +"%s")
diff=$(($date2-$date1))
echo "Time Taken - $(($diff / 60)) minutes and $(($diff % 60)) seconds elapsed."

上面的脚本打印出以分钟和秒为单位的时间。我怎样才能将它添加几个小时?这意味着它应该打印出 Time Taken - 0 hours 54 minutes 0 seconds 类似这样的内容。

最佳答案

试试这个:

echo "Time Taken - $((diff /60/60)) hours and $(($diff % 60)) minutes and $(($diff % 60)) seconds elapsed."

关于linux - 如何在 shell 脚本中将时间戳转换为小时/分钟/秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29405432/

相关文章:

shell - 在 shell 脚本中扩展 ipv6 地址

linux - 不返回命令行语法错误

linux - 是否可以确定另一个进程是否读取了您的内存?

python - 如何在不杀死 python 脚本的情况下停止它?

c - Linux 在运行时迭代 ELF "section" header

python - concurrent.futures 中的 RSS 内存使用情况

linux - 在 bash 脚本中使用 mv 丢失文件

python - 如何知道输出是否进入终端?

linux - 来自 shell 脚本内部的 nohup 命令阻止脚本执行

linux - 与 cp 命令和 -R 参数一起使用时 *.* 和 * 之间的区别