LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 07: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, 07:41 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 08: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 08:51 PM. Reason: update
 
Old 04-18-2011, 09:45 PM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
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 09:51 PM.
 
Old 04-18-2011, 11:52 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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, 08:27 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

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

Rep: Reputation: 148Reputation: 148
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, 03:35 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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 03:38 PM.
 
Old 04-19-2011, 04:40 PM   #9
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
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 04:42 PM.
 
  


Reply

Tags
bash, expansion, loop, parameter



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 02:39 PM
C command line args in linux YankeeFan Linux - Newbie 5 06-11-2006 08:22 AM
bash shell command line expansion hansi umayangan Linux - General 2 03-13-2005 11:31 AM
command line args in a countdown Squeak2704 Programming 1 04-26-2004 12:27 PM
Bash script; command and args in variable. magjo813 Programming 2 02-16-2004 09:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration