Visit Jeremy's Blog.
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Calculation using variable inside variable
 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

04-20-2015, 08:11 AM   #1
unclesamcrazy
Member

Registered: May 2013
Posts: 189

Rep:
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.

 04-20-2015, 08:37 AM #2 Keith Hedger Senior Member   Registered: Jun 2010 Location: Wiltshire, UK Distribution: Linux From Scratch, Slackware64, Partedmagic Posts: 2,380 Rep: 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.
 04-20-2015, 09:21 AM #3 allend Senior Member   Registered: Oct 2003 Location: Melbourne Distribution: Slackware-current Posts: 4,539 Rep: 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.
 04-20-2015, 09:47 AM #4 Shadow_7 Senior Member   Registered: Feb 2003 Distribution: debian Posts: 2,980 Blog Entries: 1 Rep: 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.
04-20-2015, 10:20 AM   #5
Keith Hedger
Senior Member

Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,380

Rep:
Quote:
 Originally Posted by Shadow_7 ...\$(echo \$VAR)...
Never thought of doing it that way

 04-21-2015, 02:52 AM #6 TenTenths Senior Member   Registered: Aug 2011 Location: Dublin Distribution: Centos 5 / 6 / 7 Posts: 2,380 Rep: http://centos.tips/nesting-variables-in-bash/ has a couple of examples
04-21-2015, 07:36 AM   #7
rtmistler
Moderator

Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,127
Blog Entries: 10

Rep:
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.

04-21-2015, 09:24 AM   #8
millgates
Member

Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep:
Quote:
 Originally Posted by Keith Hedger 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```

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post basusimply Programming 7 09-18-2013 03:22 PM pauld Programming 10 09-15-2011 09:33 AM muzzol Programming 9 12-01-2010 11:31 AM amemyum Linux - Newbie 13 11-16-2009 03:44 PM thekillerbean Linux - General 4 02-09-2006 08:50 PM

All times are GMT -5. The time now is 02:55 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -