2014-10-29 67 views
0

在我的腳本中,我將日期中的當前日期存儲在一個變量中,但是我希望變量能夠提前5分鐘存儲日期。在bash中將當前日期遞增5分鐘

29/10/2014-10:47:06 to 29/10/2014-10:52:06 

在Bash中有這樣一個快速簡單的方法嗎?例如,如果時間是10:59:00(例如識別到進入下一小時),那麼尋找一個也能正常工作的解決方案。

我發現了一些Perl解決方案,但沒有爲Bash。

+0

這可能適合你:'date -d'2014/10/29 10:47:06 + 5 minutes'''。或者如果它是從當前日期開始的,那麼'date -d'5分鐘''。 – 2014-10-29 10:52:34

回答

2

可以使用-d選項:

~$ date 
mercredi 29 octobre 2014, 11:45:45 (UTC+0100) 
~$ date -d '5 minutes' 
mercredi 29 octobre 2014, 11:50:55 (UTC+0100) 

如果你想使用一個變量:

~$ curr=$(date +%d/%m/%Y-%H:%M:%S) 
~$ echo $curr 
29/10/2014-11:59:50 
~$ date -d "($cur) +5minutes" +%d/%m/%Y-%H:%M:%S 
29/10/2014-12:04:54 
0

date看一看: date +%s讓你從1.1.1970秒,添加5 * 60並使用 date --date='@<newTime>'將其轉換回來。

請參閱man date bash。

1

你需要使用-d選項,與任何用戶定義的日期:

date -d '2014/10/29 10:58:06 5 minutes' 
Wed Oct 29 11:03:06 IST 2014 

爲當前日期

date -d '5 minutes' 
0

在bash,下列選項可用於date命令:

-a [-]sss.fff Slowly adjust the time by sss.fff seconds 
       (fff represents fractions of a second). This 
       adjustment can be positive or negative. The 
       system's clock is sped up or slowed down 
       until it has drifted by the number of 
       seconds specified. Only the super-user may 
       adjust the time. 

因此,使用以下內容添加5分鐘(= 300秒):

date -a 300