LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Calculation using variable inside variable (https://www.linuxquestions.org/questions/linux-newbie-8/calculation-using-variable-inside-variable-4175540209/)

unclesamcrazy 04-20-2015 08:11 AM

Calculation using variable inside variable
 
I have some variables which have stored numerical values like
Quote:

A1=10 A2=20 A3=25
B1=10 B2=15 B3=12 and so on
As well as another set of variables which stored these variables like
Quote:

var1=A1 var2=A2 var3=A3
var4=B1 var5=B2 var6=B3 and so on
I have to calculated things using second set of variable.
In the script, there is line
Code:

read number
user will enter number manually, suppose 500
I have to calculate using
Code:

expr 500 \* ${$var1} / 100
and output of this multiplied by
Code:

expr $above_output \* ${$var2} /100
and output of this multiplied by
Code:

expr $above_output \* ${$var3} /100
But not able to deal with two levels of variable. The calculation will be hard when it comes for three level and four level.

Please help.

Keith Hedger 04-20-2015 08:37 AM

If you are useing bash use the '!' indirection op to read a var like so:
Code:

A1=10
varptr=A1
echo ${!varptr}
10

You can only 'read' like this you can't use this method to set a variable, alternatly look at the eval command but it has security implications.
Code:

eval echo \$$varptr
10


allend 04-20-2015 09:21 AM

Do you really need the indirection? An alternative approach might be to use a function to do the calculation.
Code:

#!/bin/bash

myfunc() {
 c=$(echo "scale=3; $a * $b / 100" | bc -l)
 echo "a=$a b=$b c=$c"
}

a=500
for b in 10 20 25 10 15 12; do
 myfunc
 a=$c
done

As there is a division, I would use bc to retain numerical accuracy.

Shadow_7 04-20-2015 09:47 AM

If it is bash, not specified. You can $() to execute which changes the order of precedence. $(echo $VAR), which should get treated as a value at the time of operation. Also bear in mind the $(( )) for number math in bash. And bash only deals in whole numbers, if you want to retain the decimal precision you'll have to use bc or some other application. There's also declare -i VAR to let bash know that variables are numbers and not strings which might help bash make better assumptions. If it is bash that you're doing all this in.

Keith Hedger 04-20-2015 10:20 AM

Quote:

Originally Posted by Shadow_7 (Post 5350174)
...$(echo $VAR)...

Never thought of doing it that way

TenTenths 04-21-2015 02:52 AM

http://centos.tips/nesting-variables-in-bash/ has a couple of examples

rtmistler 04-21-2015 07:36 AM

The original problem here is this:
Quote:

var1=A1 var2=A2 var3=A3
var4=B1 var5=B2 var6=B3
This makes var1 be "A1" NOT "10"

So as Keith Hedger points out, you can establish pointers using the correct syntax to do this, or some of the other suggested techniques.

millgates 04-21-2015 09:24 AM

Quote:

Originally Posted by Keith Hedger (Post 5350136)
You can only 'read' like this you can't use this method to set a variable, alternatly look at the eval command but it has security implications.
Code:

eval echo \$$varptr
10


another possibility would be to use declare:

Code:

varptr=foo
declare $varptr=bar
echo $foo



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