3

I am trying to do arithmetic as I assign a variable a value. When the script reaches end_day it cannot do the math, and when I test it in the terminal it will simply return the expression. What do I need to do to get a result assigned to the variables.

Here is part of the script, $fhour comes from elsewhere and is simply a two digit number.

#Date variables
export start_year=$(date -u +%Y)
export start_mon=$(date -u +%m)
export start_day=$(date -u +%d)
export start_hour=$fhour
export end_year=$(date -u +%Y)
export end_mon=$(date -u +%m)
export end_day=${$start_day+(((($start_day*24)+84)/24)-((($start_day*24)+84)%24))}
export end_hour=${($start_day*24+84)%24}

Thanks for any help!

WxPilot
  • 1,796
  • 2
  • 18
  • 25

1 Answers1

3

Following the guide of Cyberciti for bash arithmetic you are not using the correct method. To make arithmetics operations this is the format:

$((expression))

So in your case this would be the right variable assignment:

export end_day=$(( $start_day+(((($start_day*24)+84)/24)-((($start_day*24)+84)%24)) ))
export end_hour=$(( ($start_day*24+84)%24 ))

NOTE: this need to be tested, not sure if results are correct.

Lucio
  • 18,648
  • 31
  • 107
  • 190
  • Thanks for the reply, that fixed the arithmetic problem.. I think. The rest of the script is showing its errors now, but that debugging is for another day. – WxPilot Feb 07 '14 at 23:08
  • +1 nicely done. I was working on this one too, but you beat me to it! – Aaron Feb 07 '14 at 23:09