LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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-20-2015, 09:11 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 189

Rep: Reputation: 1
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.
 
Old 04-20-2015, 09:37 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,468

Rep: Reputation: 618Reputation: 618Reputation: 618Reputation: 618Reputation: 618Reputation: 618
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

Last edited by Keith Hedger; 04-20-2015 at 09:38 AM.
 
1 members found this post helpful.
Old 04-20-2015, 10:21 AM   #3
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,658

Rep: Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539Reputation: 1539
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.
 
Old 04-20-2015, 10:47 AM   #4
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 3,300
Blog Entries: 1

Rep: Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685
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.
 
1 members found this post helpful.
Old 04-20-2015, 11:20 AM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,468

Rep: Reputation: 618Reputation: 618Reputation: 618Reputation: 618Reputation: 618Reputation: 618
Quote:
Originally Posted by Shadow_7 View Post
...$(echo $VAR)...
Never thought of doing it that way
 
Old 04-21-2015, 03:52 AM   #6
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,540

Rep: Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919
http://centos.tips/nesting-variables-in-bash/ has a couple of examples
 
Old 04-21-2015, 08:36 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,825
Blog Entries: 12

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
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.
 
Old 04-21-2015, 10:24 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by Keith Hedger View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to print a variable inside while, outside of the loop. basusimply Programming 7 09-18-2013 04:22 PM
using variable inside regex pauld Programming 10 09-15-2011 10:33 AM
[bash] re-evaluate variable inside variable muzzol Programming 9 12-01-2010 12:31 PM
can we use variable inside another variable amemyum Linux - Newbie 13 11-16-2009 04:44 PM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 09:50 PM

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

All times are GMT -5. The time now is 02:58 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration