LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-18-2011, 08:22 PM   #1
mnemry
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
BASH shell question, variable sustitution $1... command line args


Hi everyone,

I have a need to sum command line arguments in either a while or until loop. (easy to do in a for loop, but can't use one). Given the command line;

'script.sh 10 20 30 40'

I must sum the values of $1 thru $4 but having trouble getting the substitution right, I've tried

TOTAL=$(($TOTAL+$\$COUNTER))
TOTAL=$(($TOTAL+${$COUNTER}))

trying to expand $\$COUNTER to, say $1, and add the value to TOTAL but can't seem to get it right. Any advise is welcome
 
Old 04-18-2011, 08:41 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Show us your code so far. As part of a general soln, lookup the 'shift' cmd in bash...
http://linux.die.net/abs-guide/othertypesv.html
 
1 members found this post helpful.
Old 04-18-2011, 09:40 PM   #3
mnemry
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Original Poster
Rep: Reputation: 0
#!/bin/bash

# sum_until.sh
# Author: Mark Emry
# Created: 4/17/2011
# Modified: 4/17/2011,


# Purpose: until loop exercise
# Description: sums numbers in a until loop


clear
echo
echo

NUMS="$@"
TOTAL=0
TNUMS="$#"
COUNT=1

until [ $COUNT -gt $TNUMS ]
do
TOTAL=$(($TOTAL+${$COUNT}))
COUNT=$(($COUNT+1))
done

echo "$NUMS = $TOTAL"

#end

BTW, thanks for the link. I modified the script to use the shift command like so;

until [ $COUNT -gt $TNUMS ]
do
TOTAL=$(($TOTAL+$1))
shift
COUNT=$(($COUNT+1))
done

and that works just fine. Still wondering about my original methodology, is it even possible?

Last edited by mnemry; 04-18-2011 at 09:51 PM. Reason: update
 
Old 04-18-2011, 10:45 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by mnemry View Post
TOTAL=$(($TOTAL+${$COUNT}))
COUNT=$(($COUNT+1))
Looks like you are trying to use $COUNT as an index to the passed parameters. I don't think it can work that way. Instead try indirect expansion.

Code:
foo$ cat echoparms
#! /bin/bash

c=0
while [[ c -le $# ]]
do
    c=$((c+1))
    echo ${!c}
done
foo$ ./echoparms one two three
one
two
three

foo$
Of course you could always just copy all the arguments into a true Bash array first.

If I were to write a shell script to sum all arguments it would look a little different. I think what you're doing may work okay for you, but it could be simpler.

HTH

Edit
This question reminds me of some of my school assignments. For extra credit, can you tell why my loop runs one more iteration than needed?

Last edited by Telengard; 04-18-2011 at 10:51 PM.
 
Old 04-19-2011, 12:52 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,686

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
I have two queries for you:

1. Do you have to change all the standard variables ($#, $@ and so on) to new variable names?

2. You seem to use the arithmetic brackets (()) for some things but not others?

As you have provided your current solution, you may also consider:
Code:
#!/bin/bash

echo -n "$@ = "

until (( $# == 0 ))
do
    (( TOTAL += $1 ))
    shift
done

echo $TOTAL
 
1 members found this post helpful.
Old 04-19-2011, 09:27 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
You don't put $ in front of variables inside arithmetic expressions.
 
0 members found this post helpful.
Old 04-19-2011, 04:10 PM   #7
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by MTK358 View Post
You don't put $ in front of variables inside arithmetic expressions.
Maybe you don't, but Bash allows it and expressions evaluate just the same.

Code:
foo$ i=1
foo$ echo $(( i ))
1
foo$ echo $(( $i ))
1
foo$ if (( $i == 1 )) ; then echo "true" ; else echo "false" ; fi
true
foo$ i=0
foo$ if (( $i == 1 )) ; then echo "true" ; else echo "false" ; fi
false
foo$
Quote:
Originally Posted by 6.5 Shell Arithmetic - Bash Reference Manual
Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax.
http://www.gnu.org/software/bash/man...ell-Arithmetic
 
Old 04-19-2011, 04:35 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by Telengard View Post
Maybe you don't, but Bash allows it and expressions evaluate just the same.[/code]
OK, I thought it wouldn't work if you used the "$" syntax.

I also didn't know that for if statements, you can omit the "$" from in front of "((" and it will evaluate if 1 is returned. I always did it like this:

Code:
if [ $(( a == b )) '!=' 0 ]
...

Last edited by MTK358; 04-19-2011 at 04:38 PM.
 
Old 04-19-2011, 05:40 PM   #9
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by MTK358 View Post
I also didn't know that for if statements, you can omit the "$" from in front of "((" and it will evaluate if 1 is returned.
Quote:
Originally Posted by 3.2.4.2 Conditional Constructs - Bash Reference Manual
((...))

(( expression ))

The arithmetic expression is evaluated according to the rules described below (see Shell Arithmetic). If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to

let "expression"
http://www.gnu.org/software/bash/man...nal-Constructs
  • (( EXPRESSION )) yields a status based on the result of EXPRESSION.
  • $(( EXPRESSION )) expands to a value from the result of EXPRESSION.

I think I explained that right
To be fair, Bash is quite sophisticated and has many non-obvious constructs.

Last edited by Telengard; 04-19-2011 at 05:42 PM.
 
  


Reply

Tags
bash, expansion, loop, parameter


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Shell Scripting Dynamic Variable naming question ZuG Programming 2 02-07-2007 03:39 PM
C command line args in linux YankeeFan Linux - Newbie 5 06-11-2006 09:22 AM
bash shell command line expansion hansi umayangan Linux - General 2 03-13-2005 12:31 PM
command line args in a countdown Squeak2704 Programming 1 04-26-2004 01:27 PM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 10:22 AM


All times are GMT -5. The time now is 03:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration