LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 08:11 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 200

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, 08:37 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,058

Rep: Reputation: 831Reputation: 831Reputation: 831Reputation: 831Reputation: 831Reputation: 831Reputation: 831
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 08:38 AM.
 
1 members found this post helpful.
Old 04-20-2015, 09:21 AM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,119

Rep: Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604
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, 09:47 AM   #4
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
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, 10:20 AM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,058

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

Rep: Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540Reputation: 1540
http://centos.tips/nesting-variables-in-bash/ has a couple of examples
 
Old 04-21-2015, 07:36 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,773
Blog Entries: 13

Rep: Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818
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, 09:24 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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 03:22 PM
using variable inside regex pauld Programming 10 09-15-2011 09:33 AM
[bash] re-evaluate variable inside variable muzzol Programming 9 12-01-2010 11:31 AM
can we use variable inside another variable amemyum Linux - Newbie 13 11-16-2009 03:44 PM
Retrieving from a variable whose name is inside a variable. thekillerbean Linux - General 4 02-09-2006 08:50 PM

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

All times are GMT -5. The time now is 08:57 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
Open Source Consulting | Domain Registration