0

So our homework is to calculate Easter using some calculations:

p = v1+v2
v1 = (6*v2+m4+m2) mod 7
v2 = (16+m19 ) mod 30
m2 = 2*(year mod 4)
m4 = 4*(year mod 7)
m19 = 19*(year mod 19)

So these are the calculations we need to translate to Bash. This is what I have done so far with little luck:

 read -p year

 Am19=$(expr year%19)

 m19=$(19*(Am19))

 Am4=$(expr year%7)

 m4=$(19*(Am4))

 Am2=$(expr year%4)

 m2=$(2*(Am2))

 Av2=$(16+(m19))

 v2=$(expr Av2%30)

 Av1=$(6*(v2)+m4+m2)

 v1=$(expr Av1%7)

 p=$(v1+v2)

 echo "$p"

The user is supposed to input a year with the read command (like 2000) And the program should give back the number $p I get these errors

main.sh: command substitution: line 5: syntax error near unexpected token `Am19'
main.sh: command substitution: line 5: `19*(Am19))'
main.sh: command substitution: line 7: syntax error near unexpected token `Am4'
main.sh: command substitution: line 7: `19*(Am4))'
main.sh: command substitution: line 9: syntax error near unexpected token `Am2'
main.sh: command substitution: line 9: `2*(Am2))'
main.sh: command substitution: line 10: syntax error near unexpected token `m19'
main.sh: command substitution: line 10: `16+(m19))'
main.sh: command substitution: line 12: syntax error near unexpected token `v2'
main.sh: command substitution: line 12: `6*(v2)+m4+m2)'
main.sh: line 13: v1+v2: command not found

And I got no clue whats going on, Any help is always appreciated, thank you all

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

2

Arithmetic expansions uses double parentheses:

m19=$((19*(Am19)))

So, if you omit the expr, you need to double the parentheses.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • choroba, thank you friend, did what you said and the program turned out quite well but i still have errors main.sh: line 2: expr year % 19: syntax error in expression (error token is "year % 19") main.sh: line 4: expr year%7: syntax error in expression (error token is "year%7") main.sh: line 6: expr year%4: syntax error in expression (error token is "year%4") main.sh: line 9: expr Av2%30: syntax error in expression (error token is "Av2%30") main.sh: line 11: expr Av1%7: syntax error in expression (error token is "Av1%7") – Filippos Papalos Jun 05 '19 at 09:48
  • You need to expand the variables _and_ have spaces between operators: `$(expr $year % 19)` or use bash arithmetic expansion `$((year%19)` – KamilCuk Jun 05 '19 at 10:46
  • I did do that and had no issue when checking it on shellcheck, however the result keeps being the same number(21) This is the code so far, cant spot the issue: read -p year Am19=$((year% 19)) m19=$((19*(Am19))) Am4=$((year%7)) m4=$((19*(Am4))) Am2=$((year%4)) m2=$((2*(Am2))) Av2=$((16+(m19))) v2=$((Av2%30)) Av1=$((6*(v2)+m4+m2)) v1=$((Av1%7)) p=$((v1+v2)) echo "$p" – Filippos Papalos Jun 05 '19 at 13:20
  • Ask a new question if you have a new question. – choroba Jun 05 '19 at 13:36