Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script/command to print out date 5 min before/after

Tags:

bash

I need to somehow use the date command in bash or another utility to print out the date and time, 5 minutes before and 5 minutes after a given value. For example:

input:

Thu Dec 19 14:10

output:

Thu Dec 19 14:05 Thu Dec 19 14:10 Thu Dec 19 14:15

I see that the date command can be used to do this on the current date/time, can it be used with a passed value, i.e. not the current date/time?

like image 779
user117974 Avatar asked Dec 19 '13 18:12

user117974


People also ask

How do I increment a date in bash?

Use the date command's ability to add days to existing dates. Note, this works well in your case but other date formats such as yyyymmdd may need to include "UTC" in the date string (e.g., date -ud "20130515 UTC + 1 day" ).

How do I run a bash script every 5 minutes?

Easy Method You can set up a bash script that loops forever executing that command then sleeping for 5 minutes. When you start up your computer press ctrl + alt + t and type amazon-sync then minimize the terminal window. Command will run once every 5 minutes (300 seconds).

How do I print the date and time in bash?

Sample shell script to display the current date and time #!/bin/bash now="$(date)" printf "Current date and time %s\n" "$now" now="$(date +'%d/%m/%Y')" printf "Current date in dd/mm/yyyy format %s\n" "$now" echo "Starting backup at $now, please wait..." # command to backup scripts goes here # ...

How do you wait 5 seconds in bash?

How to Use the Bash Sleep Command. Sleep is a very versatile command with a very simple syntax. It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.


2 Answers

You can achieve this, for the current time, by typing.

$ date --date='5 minutes ago'; date; date --date='5 minutes'
Qui Dez 19 16:09:17 BRST 2013
Qui Dez 19 16:14:17 BRST 2013
Qui Dez 19 16:19:17 BRST 2013

To use a specific date (ex 1978/01/10).

$ date --date='1978-01-10 + 5 minutes'
Ter Jan 10 00:05:00 BRT 1978
like image 176
Rafa Avatar answered Sep 23 '22 17:09

Rafa


With GNU date, you can do a simple form of date/time arithmetic with the argument to the --date option:

$ date --date 'now + 5 minutes'

With BSD date (at least, the version that ships with Mac OS X), the -v option allows you to do something similar:

$ date -v +5M
$ date -v -5M
like image 45
chepner Avatar answered Sep 22 '22 17:09

chepner