Resolve variable inside another variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Resolve variable inside another variable
# 1  
Old 08-07-2012
Bug Resolve variable inside another variable

Hello Everyone,

I am trying to resolve a variable inside another variable.Let me go straight to the example.

Code:
Input:

Query=$Table_1 Join $Table_2

(Query itself is a variable here)

Now for two different cases I am assigning different values to Table_1 and Table_2

Code:
Case 1:
Table_1=tbl1_c1
Table_2=tbl2_c1

After this I want to write a command here so that when I echo Query it should show like:

Code:
echo $Query

tbl1_c1 Join tbl2_c1

I suppose this can be done with eval , and I have tried few options as well but I could not achieve the desired output.

Could you please help.

Thanks,
Vinay
# 2  
Old 08-07-2012
This won't work for you?
Code:
Table_1=tbl1_c1
Table_2=tbl2_c1
Query="$Table_1 Join $Table_2"
echo "$Query"

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-07-2012
I'm not quite sure what the issue you're having is. It seems the solution Bartus posted should work. For funsies though, you could probably do it like this:

Code:
(19:12:02\[root@DeCoBoxOmega)
[~/Desktop]$ tables=( "tb1|tb2" "tb3|tb4") #create an array with pairs of tables

(19:12:10\[root@DeCoBoxOmega)
[~/Desktop]$ echo "${tables[0]%%|*}" #use string manipulation to play around with one
tb1

(19:12:26\[root@DeCoBoxOmega)
[~/Desktop]$ echo "${tables[1]##*|}" #Or the other
tb4

(19:12:34\[root@DeCoBoxOmega)
[~/Desktop]$ query="${tables[0]%%|*} Join ${tables[1]##*|}" #Use these arrays to create a variable

(19:12:46\[root@DeCoBoxOmega)
[~/Desktop]$ echo ${query} #and bob's your uncle
tb1 Join tb4

(19:12:50\[root@DeCoBoxOmega)
[~/Desktop]$

This User Gave Thanks to DeCoTwc For This Post:
# 4  
Old 08-07-2012
Hi
Maybe that's what you mean ?

Code:
$ Table_1=tbl1_c1
$ Table_2=tbl2_c1
$ Query='$Table_1 Join $Table_2'
$ echo $Query
$Table_1 Join $Table_2
$ eval echo $Query
tbl1_c1 Join tbl2_c1

This User Gave Thanks to Chirel For This Post:
# 5  
Old 08-08-2012
Thanks everyone for helping me understand the basics.I used m_eval option and it worked.

I will try out other options as well and will get back if there is any problem.

Thanks again.
# 6  
Old 08-08-2012
eval is a giant security hole.

If `rm -Rf ~/` ends up in your variables somehow, eval will execute it.

Please explain what you're actually trying to do so we can show you how to avoid the eval.
# 7  
Old 08-09-2012
Corona,

I wanted to resolve a variable (say Table_1) which again is inside another varible (say Query = $Table_1 Join).Using double quotes (") I was able to it it for sample examples.

Now when I try to implement the same in the actual problem statement I have, it throws error.

Code:
 
Code:
 
resolvedQuery=echo "$mainQuery"
 
Error:
 
varibale_test.ksh[40]: <big variable name here> : name too long

And yes actually variable here is big. It is a sql query.

I tried below as well.

Code:
 
resolvedQuery=$( eval echo $(echo $mainQuery))

This works well , only problem is single quote characters are dissolved after resolution.

Example:

Code:
 
Query= $Table_1 Join $Table_2 where condition='abc'

After resolution it becomes:

Code:
 
Query=t1 Join t2 where condition=abc

Can you please help me find a way , to resolve big length variable keeping single quote (') intact.

Thanks,
Vinay
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

2. Shell Programming and Scripting

Cannot resolve $variable in awk

My script ---------- for i in `cat n`;do export k=`echo "CSN: "$i` //combining CSN: and value from n echo "$k" awk ''{print "CSN: "$0;}'{_=29}_&&_--' file1|tail -1 >> file2 done In the above script i cannot able to resolve $k in awk command file n contains ------------ 0000 1111 2222... (2 Replies)
Discussion started by: Mohana29_1988
2 Replies

3. Shell Programming and Scripting

Resolve parameter value stored in a variable

Hi All, I have below variable, xyz=\$AI_XFR Now, if you will run the below command => echo $xyz $AI_XFR It is returning hardcoded string value. Whereas in environment, there is value in it. Like below: => echo $AI_XFR /home/aditya/sandbox/xfr/ I need to resolve this... (4 Replies)
Discussion started by: adgangwar
4 Replies

4. Shell Programming and Scripting

Resolve Environment Variable

I am tyring to resolve an environment variable that is part of a string I selected from our database. Simply put, I want cd to this folder before checking if a file exists. The variable $in_loc has the value '$PS_HOME/int/VSP' where $PS_HOME is the environment variable. I am using cd... (6 Replies)
Discussion started by: consult_jb
6 Replies

5. Shell Programming and Scripting

evaluating a variable inside a variable

Hi there, i think im getting myself a little confused and need some help :wall: I am reading in a bunch of variables to my script from an external file and need to validate that a value has been set for each so if you can imagine, the user is required to pass in 4 values... (3 Replies)
Discussion started by: rethink
3 Replies

6. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

7. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

8. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

9. Shell Programming and Scripting

Resolve a Variable

Hi I have a variable which is a path ie: UBERROR=/cobwrk/mchr/prodsup/ub/wrk/../error is there anyway I can get the output of an echo to read: #echo $UBERROR /cobwrk/mchr/prodsup/ub/error instead of #echo $UBERROR /cobwrk/mchr/prodsup/ub/wrk/../error Many thanks! (2 Replies)
Discussion started by: serm
2 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question