#!/bin/csh
# This program will add integers
#
#
# add integer1 ..
#
# Check for argument
if ($#argv == 0 ) then
echo "usage: add integers"
exit 1
else
set input = $argv[*]
endif
#
set sum = 0
foreach var ( $input ) @sum = $sum + $input
end
#
echo "The sum total of the integers is $sum"
#
exit 0
#
Hi I am trying to make a program that adds all integers that are input.
ex. add 2 sum = 2
add 2 4 10 sum = 16
This is what i have for code but i keep getting the error "add: syntax error at line 18: `(' unexpected". If anyone could help me fix it i would appreciate it.
I would like to say that I came across same problem in SH script yesterday only .
#!/bin/csh -f
echo 'enter a line'
set userline = $<
echo $userline
set sum = 0
foreach var ( $userline ) @sum += $var
end
echo "The sum total of the integers is $sum "
suppose you enter 3 4 5
it will give you 12
I am not sure !! is it helpful for you or not?
As a rule in csh script
Integer calculations can be performed by C shell, using C language-type operators. To assign a calculated value, the @ command is used.
So, here we are trying to calculate . so we need "@" command instead of "$"