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.