The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Call C Program From Shell Script Chanakya.m Shell Programming and Scripting 1 05-22-2008 10:40 PM
please convert the below program into shell script mail2sant Shell Programming and Scripting 3 04-10-2008 05:39 AM
want to run different files under the same program using shell script cdfd123 Shell Programming and Scripting 3 10-04-2007 12:27 AM
Calling SHELL script from C program Chanakya.m Shell Programming and Scripting 7 09-21-2007 05:34 PM
shell script program jayaram_miryabb Filesystems, Disks and Memory 1 03-09-2005 05:12 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-14-2007
Registered User
 

Join Date: Nov 2007
Posts: 4
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.
Reply With Quote
Forum Sponsor
  #2  
Old 11-14-2007
Registered User
 

Join Date: Sep 2007
Posts: 163
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 .

sh shell user input and stote it to a array

#!/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

Last edited by user_prady; 11-15-2007 at 09:52 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0