netsize=$somenum
( echo scale=\(2; set -- ### set scale; clear args
while [ "$#" -lt "$netsize" ] ### counts up to $netsize
do set "$@" "bal$(($#+1))" ### saves arg count as bal$#+1
### I do the below because you don't say what
### node$# getbalance does. Here it's random.
eval "node$#(){ "':& x=${!%??} y=${!#"$x"}
echo "$x.$y/'"$#\"; }" ### fakes a node$#() function
printf "%bbal$#=%b" ');"' '";' \( ### );"bal$#=";(bal$#=
"node$#" getbalance ### $x.$y/$#\n
done; printf %b+ ");($1/($@))\c" ### );(bal1/(bal1+...bal$netsize))
) | paste -sd\\0 | bc ### removes all \newlines but last
So it's not at all easy to tell what you're trying to do, but one thing I noticed it that you seem to think you need to call a separate bc
for every arithmetic evaluation. The opposite is by far the more convenient way to go - bc
is an interactive program, besides being a fully-fledged scripting language all its own. It will set and store variable values for the duration of its uptime - and so rather than trying to store all of that information in the shell, just pipe out your equations and let bc
store their values for you.
For example:
{ echo 'x=1+2'; echo '"x=";x'; }| bc
...which prints...
x=3
Because first bc
evaluates x
to equal 1+2
, then it prints the arbitrary string "x="
to stdout, and last it is asked to print x
's value, and so it does.
The script above does virtually the same thing. For each iteration bal
is incremented by one. And it prints out to bc
:
);"bal$#=";(bal$#=$!/$#\n
...and then when it has incremented $#
to match $netsize
it prints (given an example $netsize
of 10)...
(bal1/(bal1+bal2+bal3+bal4+bal5+bal6+bal7+bal8+bal9+bal10))
None of those values are saved at all in the shell - bc
remembers them all, though. And when you enclose a bc
expression in (
parens )
it is evaluated and printed.
I use paste -sd\\0
to remove all \n
ewlines because I don't know what "node$#" getbalance
should do, but I assume it will follow its output with a \n
ewline, and (bal1=
node$# getbalance's output
\n)
is a syntax error. So I clear them all and separate the expressions with ;
semicolons instead (which I am rather hoping is not a char node$# getbalance
will print). To get a clear picture of what the whole script prints, you can replace the bc
at the tail w/ tr \; \\n
.
Here, for example, is what it will print if netsize=10
:
scale=(2)
"bal1="
(bal1=261.40/1)
"bal2="
(bal2=261.41/2)
"bal3="
(bal3=261.42/3)
"bal4="
(bal4=261.44/4)
"bal5="
(bal5=261.45/5)
"bal6="
(bal6=261.46/6)
"bal7="
(bal7=261.48/7)
"bal8="
(bal8=261.49/8)
"bal9="
(bal9=261.52/9)
"bal10="
(bal10=261.54/10)
(bal1/(bal1+bal2+bal3+bal4+bal5+bal6+bal7+bal8+bal9+bal10))
All of that is run through bc
which prints something like:
bal1=261.40
bal2=130.70
bal3=87.14
bal4=65.36
bal5=52.29
bal6=43.57
bal7=37.35
bal8=32.68
bal9=29.05
bal10=26.15
.34
If you can be clearer about your input and output I can help to more specifically tailor this to your needs - but doing var=$(echo stuff at | bc)
once per iteration is wasteful.
getbalance
? what doesnode$i getbalance
return, exactly? – steeldriver Jul 7 at 13:16