syntax error near unexpected token `('
Hi there,
so I've a program to define the Date of Easter Sunday for the year of choice.
Originally I've written this program in Powershell, because in school we have to use PS.
Now I have to rewrite it into a Linux Script, and I'm getting these two failure massages:
"./Input-test: line 8: syntax error near unexpected token `(
./Input-test: line 8: `$Jahr=(( 12 + "$Year" / 100 -"$Year" / 400 - ( 8* "$Year" / 100 + 13) / 25 ) %30 + 19* ( "$Year" %19 )) %30 "
I've tried a few things but my knowledge of Linux is still a bit short so I hope you guys can help me a bit.
My code:
"
#!/bin/bash
echo "Enter Year"
read Year
$Jahr=(( 12 + "$Year" / 100 -"$Year" / 400 - ( 8* "$Year" / 100 + 13) / 25 ) %30 + 19* ( "$Year" %19 )) %30
$Platzhalter = ("$Year" %17)
if["$Jahr" -eq 29]
then $D=28
elseif ["$Jahr" -eq 28 && "$Plantzhalter" -ge 11]
then $D=27
else [$D=$Jahr]
fi
fi
fi
$E=(2*("$Year"%4)+4*("$Year"%7)+6*$D+(6+"$Year"/100 -"$Year"/400 -2)%7)%7
$Tag=("$E" + "$D" +1)+21
if ["$Tag" -gt 31]
then $Tag = $Tag - 31
if ["$Tag" -lt 10]
echo "Ostersonntag fällt auf den 0$Tag.04.$Year"
else echo "Ostersonntag fällt auf den $Tag.03.$Year"
fi
fi
"
Good hunting
TheDawg
Re: syntax error near unexpected token `('
Declare your integer variables, e.g.
otherwise use let for assignments.
Don't use $ with the variable name on the lhs of =, e.g.
Every if needs a then.
Every if gets exactly one fi, e.g.
Code:
if [ expr ]
then something
elif [ expr ]
then something
else something
fi
Put double-quotes around arithmetical expressions that contain blank spaces, e.g.
Code:
Jahr="(( ((12 + $Year / 100 - $Year / 400 - ( 8 * $Year / 100 + 13 ) / 25 ) % 30 + 19 * ($Year % 19)) % 30 ))"
otherwise eliminate the spaces and precede the expression with a $
Code:
Jahr=$(( ((12+$Year/100-$Year/400-(8*$Year/100+13)/25)%30+19*($Year%19))%30 ))
(Blank spaces to offset the outer double parens (( )) are OK.)
Be sure that spelling of your variable names is consistent. (Plantzhalter?)
Re: syntax error near unexpected token `('
Moved to Programming Talk.
Re: syntax error near unexpected token `('
OP, wrap your code in [code][/code] tags, so the formatting is preserved and the script more readable.
as poster above explained, think of $ with variable names as a 'get value of'
Code:
x=1 # assignment
echo $x # getting value of x
if you want to assign expression value to a variable you need to use $ as well (another case that fits the 'get value of' line of thinking)
Code:
x=$( some_command ) # capture the output of some_command
y=$(( a/b*c+1 )) # do integer math, $s are not needed inside the integer math (( ))
conditions using [] require spaces between all parts of expression. [ is simply a command that needs a clear-cut list of parameters
[ "$var" = 1 ] => "[", "$var", "=", "1", "]"
Re: syntax error near unexpected token `('
Also, this code
Code:
[ "$Jahr" -eq 28 && "$Platzhalter" -ge 11 ]
won't work because [ is just a command as Vaphell explained above. You can either replace it with [[...]]
Code:
[[ $Jahr -eq 28 && $Platzhalter -ge 11 ]]
or split it in two commands
Code:
[ 28 -eq "$Jahr" ] && [ 11 -le "$Platzhalter" ]
But the best option IMO is to use arithmetic evaluation here
Code:
((Jahr==28 && Platzhalter>=11))
Re: syntax error near unexpected token `('
i'll add that in case of [] booleand operators AND and OR can be achieved by -a and -o.
Code:
$ Jahr=28; Platzhalter=20;
$ [ "$Jahr" -eq 28 -a "$Platzhalter" -ge 11 ] && echo true || echo false
true
$ Jahr=27; Platzhalter=20;
$ [ "$Jahr" -eq 28 -a "$Platzhalter" -ge 11 ] && echo true || echo false
false
&& and || are reserved by shell for other things (unless you go [[ ]] route which applies different logic inside the brackets)
cmd1 && cmd2 || cmd3 is an equivalent of if cmd1 then cmd2 else cmd3 (success = TRUE, failure = FALSE and the expression is evaluated according to the rules of boolean logic where you can skip calculating FALSE && ??? and TRUE || ??? ).
Re: syntax error near unexpected token `('
Oh wow,
a lot of posts since yesterday :)
So I gonna try it step by step
but thank you all so far for the response :)