递增在bash时间(分钟和秒)/ shell脚本 [英] Incrementing time (minutes and seconds) in bash / shell script

查看:570
本文介绍了递增在bash时间(分钟和秒)/ shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要增加分和秒(相对于时间)的变量。

I need to increment minutes and seconds (in relation to time) in a variable.

首先,我不知道是否宣告时间变量被写成

First, I'm not sure whether declaring a 'time' variable is written as

time="00:00:00" 

 time=$(date +00:00:00)?

从那里,我想了10分钟,并导致搜索结果01:00:00秒递增这个变量增加到结果1点10分10秒到
1时20分二十○秒等(至午夜十二时一路 - 00:00:00)?_爱什么是最好的方式实现这一目标。

From there, I want to increment this variable by 10 minutes and seconds resulting in

01:00:00 increased to
01:10:10 to
01:20:20 etc (all the way up to midnight - 00:00:00)

What would be the best way to achieve this?

据我了解,做
$日期-D2010-07-07 200天的添加(200)天,但我不知道怎么这个例子应用到时间(分钟和秒),并没有约会?

I understand that doing $ date -d "2010-07-07 200 days" adds (200) days but I don't know how to apply this example to time (minutes and seconds) and not date?

所有答复都多少AP preciated。

All replies are much appreciated.

推荐答案

您可以使用划时代的时间 - 即秒自1970年1月1日00:00:00,例如:

You could use epoch time - i.e., seconds since 1 Jan 1970 00:00:00, for example:

#!/bin/bash

time=0
echo `date -d "1970-01-01 00:00:00 UTC $time seconds" +"%H:%M:%S"`
time=$((time + 600))
echo `date -d "1970-01-01 00:00:00 UTC $time seconds" +"%H:%M:%S"`
time=$((time + 600))
echo `date -d "1970-01-01 00:00:00 UTC $time seconds" +"%H:%M:%S"`

给出了这样的输出:

gives this output:

$ /tmp/datetest.sh
00:00:00
00:10:00
00:20:00
$

这篇关于递增在bash时间(分钟和秒)/ shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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