Yaron's answer helps you understand and remove the syntax errors.
My answer uses some 'nicer' syntax to do the same thing and something else, that might be what you want.
#!/bin/bash
read -p "enter a number: " n
s=0
i=1
j=0
while [ $i -le $n ]
do
if [ $(( i % 2 )) -eq 0 ]
then
s=$(( s + i ))
j=$(( j + 1 ))
fi
i=$(( i + 1 ))
# uncomment: remove the '#' from the beginning of the line
# echo "i=$i" # uncomment to get debug output
done
#echo "n=$n" # uncomment to get debug output
#echo "j=$j" # uncomment to get debug output
#echo "s=$s" # uncomment to get debug output
echo "Is this what you want?"
echo "sum of $j even numbers ( <= $n ) = $s"
echo "or is this what you want?"
s=0
for ((i=1;i<=n;i++))
do
echo -n "$(( 2*i )) "
s=$(( s + 2*i ))
done
echo ""
echo "sum of $n even numbers = $s"
Running test examples,
$ ./sum-of-even-numbers
enter a number: 3
Is this what you want?
sum of 1 even numbers ( <= 3 ) = 2
or is this what you want?
2 4 6
sum of 3 even numbers = 12
$ ./sum-of-even-numbers
enter a number: 4
Is this what you want?
sum of 2 even numbers ( <= 4 ) = 6
or is this what you want?
2 4 6 8
sum of 4 even numbers = 20
$ ./sum-of-even-numbers
enter a number: 6
Is this what you want?
sum of 3 even numbers ( <= 6 ) = 12
or is this what you want?
2 4 6 8 10 12
sum of 6 even numbers = 42