Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Need help on C-shell script program

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-14-2007
haze21 haze21 is offline
Registered User
 
Join Date: Nov 2007
Last Activity: 17 November 2007, 3:24 PM EST
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Need help on C-shell script program

#!/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.
Sponsored Links
    #2  
Old Unix and Linux 11-14-2007
user_prady user_prady is offline
Registered User
 
Join Date: Sep 2007
Last Activity: 6 November 2008, 4:05 AM EST
Posts: 163
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
Originally Posted by haze21 View Post
#!/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 .

http://www.unix.com/shell-programmin...ote-array.html

#!/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?
Quote:
Originally Posted by haze21 View Post
What am I doing wrong with this foreach loop?

foreach var ($argv)
@sum = $sum + $var
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 "$"

User_prady Linux

Last edited by user_prady; 11-15-2007 at 11:52 PM..
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
shell script program usersnehal Shell Programming and Scripting 3 03-21-2011 05:39 AM
Calling a shell script from a C program swasid Programming 9 07-12-2010 07:19 AM
Calling a shell script from a C program swasid Shell Programming and Scripting 0 07-09-2010 07:21 AM
How to use shell script's to get EPG Program with SED single Shell Programming and Scripting 3 11-07-2007 11:56 AM
shell script program jayaram_miryabb Filesystems, Disks and Memory 1 03-09-2005 07:12 AM



All times are GMT -4. The time now is 01:09 PM.