LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with a date within a script (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-a-date-within-a-script-760561/)

xmdms 10-08-2009 09:10 AM

Need help with a date within a script
 
Hi,

I am trying to write this script to monitor a log file within the last 10 lines and take the current time and subtracted by 15 minutes. I am stuck at the date calculation part.

date
Thu Oct 8 09:08:50 CDT 2009

I would like to know the syntax on how to take this time current time and subtracted by 15 minutes.

Help!

Thank you in advance.

J

yancek 10-08-2009 09:39 AM

What scripting language are you using? It would probably be easiest to use the unix timestamp to do the subtraction then convert back to the date format you want.

tredegar 10-08-2009 09:41 AM

I hope this isn't homework.

date can easily do this for you.

Try date -d "now -15 minutes"

Doculus 10-08-2009 09:43 AM

Try this:

Code:

date -d @$[$(date +%s) - (15*60)]

TB0ne 10-08-2009 09:45 AM

Quote:

Originally Posted by xmdms (Post 3712282)
Hi,

I am trying to write this script to monitor a log file within the last 10 lines and take the current time and subtracted by 15 minutes. I am stuck at the date calculation part.

date
Thu Oct 8 09:08:50 CDT 2009

I would like to know the syntax on how to take this time current time and subtracted by 15 minutes.

Well, what kind of script are you writing? Bash? Perl? Python?

The date command can do this for bash, read the man page. For example:
Code:

date --date="-1 days" +%Y%m%d
will bring you yesterdays date, in YYYYMMDD format.

Doculus 10-08-2009 09:45 AM

Until I tried mine, we got the real answer;)


All times are GMT -5. The time now is 04:19 AM.