Joey
Joey

Reputation: 164

linux how to use dynamic variables

I have made a shell script that does some calculations. The user inputs 2 numbers: the first number being the month (if desired date is february 2010 for example he puts in 2) the second number being the year (if desired date is february 2010 for example he puts in 2010)

My script will then calculate the number of days that have passed from every day in januari 2000 to the date the user has inputed using the following code.

EDIT (had some stupid syntax errors in my code)

a=$(echo "(14-$1)/12" | bc)
y=$(echo "$2 + 4800 - $a" | bc)
m=$(echo "12 * $a - 3 + $1" | bc)
jdn=$(echo "dd + ((153 * $m +2)/5) + (365 * $y) + ($y/4) - ($y/100) + ($y/400) - 32045" | bc)

Because there are 31 days in a month (yes in my script I will just assume every month has 31 days) my "dd" variable in the last line of code will change 31 times.

I wonder how to do this without copy pasting the formula 31 times changing the code each time.

Upvotes: 0

Views: 73

Answers (1)

Matt
Matt

Reputation: 1308

It could be something like that:

a=$((14-mm)/12 | bc)
y=$(yyyy + 4800 - $a | bc)
m=$(12 * $a - 3 + mm)

for dd in $(seq 1 31);
do
   jdn=($dd + (153 * $m +2)/5 + 365 * $y + $y/4 - $y/100 + $y/400 - 32045)
done    

Upvotes: 4

Related Questions