I am using variables to state the start time and date for an at job. This works fine, but now I realize I need to subtract a number of hours from the time variable to make the at job run earlier than is stated in the variable (this variable is used elsewhere in the script as well as for the at job)


#I need to subtract 4 hours from the time variable 
at -f /home/JS/filename $time-4hours $date >attemp 2>&1

I thought about introducing a 3rd variable as the result of the subtraction but can't get that going either.

  • you need to quote the time/date argument. without quotes, it's two arguments, not one. try at -f /home/JS/filename "$time -4 hours $date" >attemp 2>&1 – cas Jan 16 '18 at 15:18
  • 12hours-based timestamps will be more complicated to handle than 24hours-one. Can you at least change that or not at all? Are you guaranteed that the delta of 4 hours will not let you roll back to previous day? – Patrick Mevzek Jan 16 '18 at 15:24
  • On 24hours arithmetic and depending on your platform (should be ok on any GNU based ones), this could work: time=1400 ; date --date="$time -4 hours" +%H%M which outputs 1000 – Patrick Mevzek Jan 16 '18 at 15:44
  • @PatrickMevzek at has pretty sophisticated date/time parsing. IIRC, it was doing things like now + 2 hours and 4pm tomorrow long before GNU date added that capability. I doubt if it's going to be confused by a 12 hour clock, as long as the am/pm indicator is included. – cas Jan 16 '18 at 16:00

On my Debian system:

$ echo true | at 2:00pm 2018-01-18 - 4 hours
warning: commands will be executed using /bin/sh
job 10 at Thu Jan 18 10:00:00 2018

So, given appropriate values in time and date, I think this should work:

$ at -f ... $time $date - 4 hours

It's a bit picky about the order of the tokens, it seems they need to be in order time, date, delta. E.g. this doesn't work:

$ echo true | at 2:00pm - 4 hours 2018-01-18
syntax error. Last token seen: 2018-01-18
Garbled time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.