Calling a variable of variable from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling a variable of variable from a file
# 1  
Old 08-04-2016
Calling a variable of variable from a file

Hi All,

I have file which have looks like below

Code:
abc=${def}
def=${efg}
efg= "this is the actual value"


based on "abc" value I have to call "efg" value , Am using below lines but it is not working

Code:
#!/bin/bash
source file.txt
echo $abc



Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags as required by the forum rules.
It makes them far easier to read and preserves multiple spaces for indenting or fixed width data.

Last edited by rbatte1; 08-04-2016 at 06:11 AM.. Reason: Code tags
# 2  
Old 08-04-2016
The sequence of the file you source in is important. When abc is set, the value of def is still Null.

When def is set, the value of efg is still Null too.

Trying to set variable efg like this will likely cause an error anyway because of the space.

I assume you would need to do this:-
Code:
efg="this is the actual value"
def=${efg}
abc=${def}

You can then run the script and should get your output.



I hope that this helps,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 08-04-2016
Indeed, it IS working. Exactly as expected from the specifications of the bash shell. It may not do what you intend to do, though. Please explain more explicitly what your intentions are.

Did you consider "indirect expansion" ( ${!var} ) that recent bash versions offer, or "associative arrays"?
# 4  
Old 08-04-2016
Quote:
Originally Posted by RudiC
Did you consider "indirect expansion" ( ${!var} )
In this case I'd say the same as i thought when i read the first post: there must have happened a serious flaw in the design process before to make such outrageous means necessary. This is the same as the old:

Code:
eval \$${var}

which should equally be avoided like the plague. Life is too short to waste it chasing evals once they go haywire (which the always do sooner or later).

I hope this helps.

bakunin
# 5  
Old 08-04-2016
No, does not help, sorry.
Code:
echo "${!var}"

is nicer than
Code:
eval echo \"\$$var\"

especially if more context is added.
Sourcing shell code means you must trust the external source.
Sourcing variables is only a little safer, you must still trust it.

Back to the original problem, here is an elegant method
Code:
abc=def
def=efg
efg="this is the actual value"

varvar(){
newres=$1
while [ -n "$newres" ]
do
  res=$newres
  newres=${!res}
done
echo "$res"
}

varvar abc
varvar def
varvar efg

# 6  
Old 08-04-2016
Quote:
Originally Posted by MadeInGermany
No, does not help, sorry.
Code:
echo "${!var}"

is nicer than
Code:
eval echo \"\$$var\"

especially if more context is added.
I beg to respectfully differ: both are great methods to shoot oneself into foot, even if i have to concede that your method is doing so with a nicer looking gun.

Perhaps the only reason to use such an indirect evaluation (regardless of using your construct or mine) is because the function doesn't know (at writing time) the variables true name. This establishes a sort-of passing by reference (instead of the usual passing by value).

To give an example for what i mean, if you have this code:

Code:
x=value
func "$x"

then inside the function you "know" the name of your received variable - $1:

Code:
func()
{
parm1="$1"

do_something "$parm1"
....
}

Whereas here:

Code:
x="value"
func x    # notice: variable name instead of value

func()
{
parm1=${!1}       # your way
eval parm1=\$$1  # my way

...
}

You have not the same kind of knowledge because you cannot expand $1 but have to expand what $1 expands to.

In languages such as C you pass around pointers to data (or, in FORTRAN, you pass by reference) because you want to manipulate the data statically across functions: one function creates a list and passes a pointer to it (instead of the copied structure) to another function which adds a new element. Both functions work on the same data instead of one calling the other with a copy of the original (which would be lost when the function returns).

But in scripting languages there is no such thing like pass by reference and there is no such thing as common, static data you can manipulate. You can try to dot-execute all your functions to get there, but this would most probably lead to desaster quite fastly. So there is no real application for this kind of "indirect evaluation" other than perhaps supporting bad design. I am sure that - by reformulating the problem - it would perhaps be possible to come up with a simpler and cleaner solution which will not rely on double dereferencing to get to a variables content.

And there is one more problem: if you look closely at the second variation with the double dereference you will notice that it relies on the existence of a variable named "x" which is defined outside, in the calling function. This is a violation against the rule of encapsulation: you should use at one level only what you defined on that level, not something from the background environment, because once this environment changes you are in deep kimchi.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling specific characters from a find variable

I'm trying to do something like this: find . -name blablabla -exec ln -s ./"{:53:14} blablabla" \; The idea is find blablabla and create a symbolic link to it using part of it's path and then it's name, "blablabla." I just don't know if I can call characters out of a find variable. ... (16 Replies)
Discussion started by: scribling
16 Replies

2. Shell Programming and Scripting

Calling a Variable based on a Variable

Hi all, I have a source config file with variables like so: eth1_ip=192.168.1.99 eth2_ip=192.168.1.123 eth3_ip=172.16.1.1 I am trying to run a script which loops based on the number of eth interfaces on a machine and therefore modifies the variable it calls in the environment based on the... (5 Replies)
Discussion started by: landossa
5 Replies

3. UNIX for Dummies Questions & Answers

Calling a variable from a variable.

Hi everyone, Is it possible to set a variable that calls another variable? I.E. SCRIPT=MY_SCRIPT.ksh ${VAR5} ${VAR5} is set earlier in the script, and I want to be able to call this when setting the ${SCRIPT} variable. I hope this makes sense. Thanks for your help. (3 Replies)
Discussion started by: jimbojames
3 Replies

4. Shell Programming and Scripting

Csh variable calling problem

First post on here. So I use csh shells for my research (physics... not a CS person). I am trying to rerun the same scripts, but there are ~10 files that have similar variables that I have to change for each different configuration, so I would like one central file for the variables I change that... (3 Replies)
Discussion started by: sabrepride
3 Replies

5. Shell Programming and Scripting

Calling a variable in another variable

echo "$previous_tmp$i" I have a 5 variables like previous1 previous2 previous3 previous4 previous5 I want to use a for loop to call them one by one. How can I ?:confused: (2 Replies)
Discussion started by: Junaid Subhani
2 Replies

6. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

7. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

8. UNIX for Dummies Questions & Answers

Calling a function through a variable

Hey folks, I'm pretty new to unix programming. I was trying to get something to work but it's not doing what I expected. #!/bin/ksh . ./functions.sh STRING=function_1 FUNCTION="$STRING" RETURN=eval $FUNCTION echo "value of $FUNCTION function is: $RETURN" All i'm... (5 Replies)
Discussion started by: Irrational
5 Replies

9. Shell Programming and Scripting

calling a variable to echo to a log

Hi everyone, I am trying to create a simple batch file to make SQL backups. this part of it works fine. Currently the script can mysql dump the databases, compress them, delete the .sql, compress the individual tar.gz into one larger one, delete the smaller files, encrypt the final tar.gz and... (1 Reply)
Discussion started by: luma
1 Replies

10. Shell Programming and Scripting

calling a aliased variable

Issue: i have variable A which is an alias for variable B which is equal to "THIS IS A TEST" when every i echo variable A i only get the alias name for variable B, NOT the contents of variable B. HOSTNAME# echo $TESTIT + echo THIS IS A TEST THIS IS A TEST HOSTNAME# ls -l total... (10 Replies)
Discussion started by: Optimus_P
10 Replies
Login or Register to Ask a Question