Variable issues

Hey guys,

I have just started getting into shell scripting, ive been self educating myself with it and have run into a snag.

I am trying to make a very simple addition script. The script would be passed a number of parameters (numbers) and it would add them all together. I can do this fine when there is a set number of parameters but when they are undefined I have trouble.

Here is what I have so far and its output.

#!/bin/csh

if ( $#argv <= 1 ) then
   echo Usage: 2 parameters are required
   exit 1
endif

echo $#argv numbers were entered

set totalparameters = $#argv

  @ sum = $1 + $2

while ($totalparameters > 0)
  shift
  @ totalparameters --
  @ sum = $sum + $2
  echo $sum
end
robert-desktop:~/bin> argvexample 1 2 3 2
4 numbers were entered
6
8
@: Expression Syntax.
robert-desktop:~/bin> 

It is adding the numbers but its throwing an error. Im totally lost.

If you can help I would really appreciate it. Thanks in advance.

well im no csh expert but this will do the trick

#!/bin/csh

if ( $#argv <= 1 ) then
   echo Usage: 2 parameters are required
   exit 1
endif

echo $#argv numbers were entered

set totalparameters = $#argv

 # @ sum = $1 + $2 #if you want to sum, no need for this one...

while ($totalparameters)   # removed < 0 since this will do the same
  @ totalparameters --
  @ sum = $sum + $1
  echo $sum
  shift     #shift after! or you end up with doing one shift to much.
end

hope this helps!

Cheers

Thanks so much, did the trick.