0

In my script, the following code successfully produces a timestamp variable for $n in the format 2016-04-28T15:47:48

for n in $(perl parsetime.pl | sed "s/.....$//")

do
echo $n

result is:

2016-04-28T15:47:48

However, I now want to use this variable to calculate the time 15 minutes earlier. Someone else provided me the syntax to produce the timestamp in the correct format (which worked)-this was achieved like this:

/opt/bin/date --date '-15 minutes 2016-04-28T15:39:27' "+%Y-%m-%dT%H:%I:%S"

result is:

2016-04-28T09:09:27

However, my issue is now, when I try to use the $n variable instead of writing out the actual timestamp I get the message like this I get an error message.

for n in $(perl parsetime.pl | sed "s/.....$//")

do

/opt/bin/date --date '-15 minutes "$n"' "+%Y-%m-%dT%H:%I:%S" 

result:

/opt/bin/date: invalid date `-15 minutes "$n"

What am I doing wrong? How can I incorporate the $n variable into the code correclty?

  • The (nearly) free-form date entry following the --date switch, is only available in the GNU date. Are you sure /opt/bin/date is the GNU date command ? – MelBurslan Apr 28 '16 at 16:07
  • Ah, would that explain why the -15 calc isn't calculating the correct time? (its calculating 7 hours before the said date). How would I resolve this? Sorry I'm a bit of a noobie! – neilH Apr 28 '16 at 16:49
1

The problem is here:

'-15 minutes "$n"'

Single quotes stop variable substitution, so you're literally passing "$n" in rather than the contents of the variable.

Write:

/opt/bin/date --date "-15 minutes $n" '+%Y-%m-%dT%H:%I:%S'

instead.

  • Thanks, this worked, although I now have another issue! For some reason the time calc isn't calculating 15 mins before, it's calculating 7 hours for some reason- I'm about to create a new question. – neilH Apr 28 '16 at 16:46

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.