cancel
Showing results for 
Search instead for 
Did you mean: 

Help to do a script

Help to do a script

Hello gurus,
I'm trying to make a script by using shell. I would like to substract 1 hour to the current date and to get the result in this format (Month Day Hour):

Mar 10 18 (It is March 10 at 19 hours)
Mar 9 23 (It is March 10 at 00 hours)

My variable is:
i=`date +'%-3.4h %2.1d %H'`

How can i substract 1 hour to my variable $i in shell?
Christian Aguilar
4 REPLIES
Ivan Krastev
Honored Contributor

Re: Help to do a script

Mark McDonald_2
Trusted Contributor

Re: Help to do a script

You could also move the TimeZone by an hour before running i=`date +'%-3.4h %2.1d %H'`

James R. Ferguson
Acclaimed Contributor

Re: Help to do a script

Hi:

Use Perl;

# i=$(perl -MPOSIX -le 'print strftime "%h %2d %H",localtime(time-3600)')

# echo ${i}
Mar 10 18

Use the same date formatting directives that you are accustomed to using --- see the 'date' manpages if you like.

This above code handles one-hour (3600 second) transitions across months and years without any other effort.

Regards!

...JRF...



Dennis Handly
Acclaimed Contributor

Re: Help to do a script