Sponsored Content
Full Discussion: Variable issues
Top Forums Shell Programming and Scripting Variable issues Post 302250771 by bert682 on Friday 24th of October 2008 07:47:31 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
shells(4)							   File Formats 							 shells(4)

NAME
shells - shell database SYNOPSIS
/etc/shells DESCRIPTION
The shells file contains a list of the shells on the system. Applications use this file to determine whether a shell is valid. See getuser- shell(3C). For each shell a single line should be present, consisting of the shell's path, relative to root. A hash mark (#) indicates the beginning of a comment; subsequent characters up to the end of the line are not interpreted by the routines which search the file. Blank lines are also ignored. The following default shells are used by utilities: /bin/bash, /bin/csh, /bin/jsh, /bin/ksh, /bin/pfcsh, /bin/pfksh, /bin/pfsh, /bin/sh, /bin/tcsh, /bin/zsh, /sbin/jsh, /sbin/sh, /usr/bin/bash, /usr/bin/csh, /usr/bin/jsh, /usr/bin/ksh, /usr/bin/pfcsh, /usr/bin/pfksh, /usr/bin/pfsh, and /usr/bin/sh, /usr/bin/tcsh, /usr/bin/zsh. Note that /etc/shells overrides the default list. Invalid shells in /etc/shells may cause unexpected behavior (such as being unable to log in by way of ftp(1)). FILES
/etc/shells lists shells on system SEE ALSO
vipw(1B), ftpd(1M), sendmail(1M), getusershell(3C), aliases(4) SunOS 5.10 4 Jun 2001 shells(4)
All times are GMT -4. The time now is 12:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy