Calculation using variable inside variable
I have some variables which have stored numerical values like
Quote:
Quote:
In the script, there is line Code:
read number I have to calculate using Code:
expr 500 \* ${$var1} / 100 Code:
expr $above_output \* ${$var2} /100 Code:
expr $above_output \* ${$var3} /100 Please help. |
If you are useing bash use the '!' indirection op to read a var like so:
Code:
A1=10 Code:
eval echo \$$varptr |
Do you really need the indirection? An alternative approach might be to use a function to do the calculation.
Code:
#!/bin/bash |
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.
|
Quote:
|
http://centos.tips/nesting-variables-in-bash/ has a couple of examples
|
The original problem here is this:
Quote:
So as Keith Hedger points out, you can establish pointers using the correct syntax to do this, or some of the other suggested techniques. |
Quote:
Code:
varptr=foo |
All times are GMT -5. The time now is 02:20 PM. |