Sponsored Content
Top Forums Shell Programming and Scripting Need help on C-shell script program Post 302145608 by user_prady on Wednesday 14th of November 2007 08:04:56 PM
Old 11-14-2007
Quote:
Originally Posted by haze21
#!/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 .

https://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
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 Smilie

Last edited by user_prady; 11-16-2007 at 12:52 AM..
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

shell script program

shell script for sorting,searchingand insertion/deletion of elements in a list (1 Reply)
Discussion started by: jayaram_miryabb
1 Replies

2. Programming

How to include shell script in C program

hi I want to call a shell script in C program the script is : ssh -t user@remote sh /<remote>home/user/<file_name>.sh and other several commands C program : Call this script and the retrive the task that is been done in <file_name>.sh file can any one tell me how... (5 Replies)
Discussion started by: mridula
5 Replies

3. Shell Programming and Scripting

Call C Program From Shell Script

Hi, Could anybody please let me know how to call a C_Program from shell script. I know through command "system" we can call shell script from C program. Awaiting response. Thanks and regards, Chanakya M (4 Replies)
Discussion started by: Chanakya.m
4 Replies

4. Shell Programming and Scripting

program for multiplication in shell script

Hi, I wanted to write a schell program that fetches the values from a file and prints the output as its onerall multiplication. for example I have a file named abc. it has values 2, 3, 4 now my program should give me 2*3*4 ie 24. note:this file abc can have any numbers. so experts,... (9 Replies)
Discussion started by: sandeep.krish
9 Replies

5. Shell Programming and Scripting

Shell Script to launch C program

Hi there, im new too shell scripting and was wondering if it is possible to create a shell script to take in a variable and load a c program. My C program is a file monitor, and is started by using the terminal and using to following code ./monitor FileToBeMonitored is it possible to have... (12 Replies)
Discussion started by: gazmcc182
12 Replies

6. Shell Programming and Scripting

Starting a C++ program from shell script

Hi, I have a little problem...I want to do the following things: Have my own little script that has 2/3 functions and starts a c++ application using some parameters. The problems appear when I start the c++ app using the shell script, the c++ takes over and after I ctrl+c the script... (0 Replies)
Discussion started by: valiadi
0 Replies

7. Shell Programming and Scripting

shell script program

shell script in Unix/Linux to find the lines numbers of a text file are having word which is 5 to 10 characters long and having first letter as a capital letter. (3 Replies)
Discussion started by: usersnehal
3 Replies

8. Shell Programming and Scripting

Killing a program in shell script

I followed the directions here Free Twitter Source Code ? Twitter Database Server: Install and created a php script that enters twitter keyword searches into a MySQL DB. When you execute the files outlined in the above link, a script starts indefinitely. I've noticed that the scripts... (6 Replies)
Discussion started by: phpchick
6 Replies

9. Homework & Coursework Questions

Need help writing a shell script program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write the a shell script program to remove all space characters stored in the shell variable TEXT.... (7 Replies)
Discussion started by: kofine05
7 Replies

10. Shell Programming and Scripting

Converting a shell script to program

Hi I am new in programming. I have written a shell code, but i want to secure my code. I have tried SHC. It is converting it to binary, but can be converted in plain text again by core dump. I have tried to convert it in rpm by "rpmbuild -bb my.spec" option but the result is same. ... (4 Replies)
Discussion started by: Priy
4 Replies
SUM(1)								   User Commands							    SUM(1)

NAME
sum - checksum and count the blocks in a file SYNOPSIS
sum [OPTION]... [FILE]... DESCRIPTION
Print checksum and block counts for each FILE. -r use BSD sum algorithm, use 1K blocks -s, --sysv use System V sum algorithm, use 512 bytes blocks --help display this help and exit --version output version information and exit With no FILE, or when FILE is -, read standard input. AUTHOR
Written by Kayvan Aghaiepour and David MacKenzie. REPORTING BUGS
Report sum bugs to bug-coreutils@gnu.org GNU coreutils home page: <http://www.gnu.org/software/coreutils/> General help using GNU software: <http://www.gnu.org/gethelp/> COPYRIGHT
Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO
The full documentation for sum is maintained as a Texinfo manual. If the info and sum programs are properly installed at your site, the command info coreutils 'sum invocation' should give you access to the complete manual. GNU coreutils 7.1 July 2010 SUM(1)
All times are GMT -4. The time now is 08:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy