Variable issues


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variable issues
# 1  
Old 10-24-2008
Variable issues

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.

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

Code:
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.
# 2  
Old 10-24-2008
well im no csh expert but this will do the trick

Code:
#!/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 #if you want to sum, no need for this one...

while ($totalparameters)   # removed < 0 since this will do the same
  @ totalparameters --
  @ sum = $sum + $1
  echo $sum
  shift     #shift after! or you end up with doing one shift to much.
end

hope this helps!

Cheers
# 3  
Old 10-24-2008
Thanks so much, did the trick.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

2. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

3. Shell Programming and Scripting

Awk Issues - Not printing the 10th Variable.

All, I am attempting to print the tenth ($COPY2) varaibales into one file. But i am finding that all variables are being outputted except for $10. Can someone help!!!! Code Below ---------- echo $SERVER $IMAGE $IMAGEDAY $IMAGEMONTH $IMAGEYEAR $COPY1 $EXPIREDAY $EXPIREMONTH... (1 Reply)
Discussion started by: Junes
1 Replies

4. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

5. Solaris

Scripiting quote and variable issues

Hello all I have an interesting problem here that I can't seem to figure out. Note this exists inside a script with the following header: m#!/usr/bin/sh What I am trying to do is build an automounting script for USB hdd's, the idea is that the user plugs their drive in then runs the script. The... (5 Replies)
Discussion started by: striker0010
5 Replies

6. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

7. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Shell Programming and Scripting

variable issues

Hope someone can help me. I have 2 outputs 1 2 3 4 5 a b c d e basically, I'd like to loop thru these outputs and print out the results below 1/a 2/b 3/c 4/d 5/e Thanks, (16 Replies)
Discussion started by: kkkk
16 Replies

10. Shell Programming and Scripting

if [ -f "$variable" ]; then issues, help!

Im trying to write a /bin/bash script that refreshes VMware Fusion using the built in snapshot capabilities. I am having an issue getting a variable to pass into the find argument of an "if-then" statement. Im thinking the problem might have something to do with the working directory of the... (8 Replies)
Discussion started by: afriend
8 Replies
Login or Register to Ask a Question