Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mathematic expressions...

Status
Not open for further replies.
Feb 14, 2002
88
JP
I know the syntax (I thought anyway).
basically what I've got is a list of integers that I can successfully bring into the file.
What I want to do is add them all together. What I *THOUGHT* would work, is:

list=list.txt
numbers=`cat $list`
typeset -i increment; increment=0
teypset -i total; total=0

for increment in $numbers
do
(( $total = $total + $increment ))
echo $total
done
exit

But I always get a:
expression requires lvalue error.

Sorry if this is a ridiculously inane question. I've never done any arithmetic with shells before.
 
you have a misspelling; teypset should be typeset
you should change your addition line to a "let" statement
note the shorthand for initializing vars using typeset

list=list.txt
numbers=`cat $list`
typeset -i increment=0
typeset -i total=0

for increment in $numbers
do
let "$total = $total + $increment"
echo $total
done
exit
 
Hi,

within let or '(( ))', you must not specify the $ for variable substitution :

let "total = total + increment"

((total = total + increment))

((total += increment)) Jean Pierre.
 
An easier way (I've done it in ksh) would be ..

list=list.txt
numbers=`cat $list`

for increment in $numbers
do
total=$((total + increment))
echo $total
done
exit

Greg.
 
THanks guys... I really hate when I've got those one word screw-ups. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top