Resolve variable inside another variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Resolve variable inside another variable
# 8  
Old 08-09-2012
Quote:
Originally Posted by vinay4889
Corona,

I wanted to resolve a variable (say Table_1) which again is inside another varible (say Query = $Table_1 Join).
Depending on the shell, you can do this with ${!VARNAME}. BASH or new versions of ksh can do this.

I still don't know why you want to do that, though.

You haven't explained your actual problem -- just the way you're hellbent on solving it. The kind of thing you're trying to do is something that's usually avoided, and for good reason.

As such, it's very difficult to offer helpful advice.
# 9  
Old 08-09-2012
Corona,

Thanks for the reply.
I am sorry that you had ask it again Smilie. I thought instead of explaining the whole problem statement , it would be good if I just ask what is it that I am facing problem which is resolving parameters.

Here is the actual problem statement I am trying to solve.

I have a SQL query. This query has variables which I want to resolve at run-time.Query is itself a variable. And the parameters(variables inside it) for this Query will be assigned values just before I write the command(This command I am struggling with) to resolve the Query.

Once the Query is resolved I will put that in command line (m_db command).

During this process I have faced problem which I stated.I hope my previous post will make you sense now.

Could you please help me out with below two problems:
1. How to overcome this error : variable name too long
2. How to retain single quote character while resolving variable.
(I have given detail with example in previous post)

I am using Korn Shell.

Thanks,
Vinay
# 10  
Old 08-09-2012
Quote:
Originally Posted by vinay4889
I have a SQL query. This query has variables which I want to resolve at run-time.
Smilie Why do you want to resolve variables inside variables?

The way to avoid the inevitable problems of that kind of doublethink is to not do that. You're finding it ugly and fraught with problems because it is ugly and fraught with problems. There's not a "right way" -- you're twisting the shell in directions it's not supposed to go.

If you describe the problem you're actually trying to solve -- the thing your program is actually supposed to do! -- there's probably a way which avoids the need for that.
# 11  
Old 08-09-2012
Corona,

I tried to explain my actual purpose in previous post.
Let me go to the problem statement directly.

Suppose this is the Query.

Code:
echo "$Query"

Select '$ENV',t1.field1,t2.field2
from $table1 t1,$table2 t2
where t1.field1='$var1'
and t2.field1='$var2'

This Query is actually big (30+ lines)

Now I have below assignments of variables:

Code:
ENV='DEV'
table1='ABC'
table2='XYZ'
var1='Hi'
var2='Bye'

Then I use below command to resolve:

Code:
resolvedQuery= echo "$Query"

I am expecting below output:

Code:
echo $resolvedQuery

Select 'DEV',t1.field1,t2.field2
from ABC t1,XYZ t2
where t1.field1='Hi'
and t2.field1='Bye'

But it throws below error:

Code:
variable name too big

And if I try below:

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

It resolved without error but then I have lost single quote character in query.
Please see below output for clarification.

Code:
Select DEV,t1.field1,t2.field2
from ABC t1,XYZ t2
where t1.field1=Hi
and t2.field1=Bye

I hope I have made my case now Smilie

Please advise.

Thanks

Last edited by vinay4889; 08-09-2012 at 06:26 PM..
# 12  
Old 08-09-2012
You either can't or won't tell me what the purpose of your program is. Nobody writes a program to "substitute in a variable", presumably that program has some actual use to you.

Fine, I'll do what I can without it.

You are doing it exactly backwards, which is why you are having so many problems. Set the variables first. Then make your query, letting it substitute the variables right then and there. That is the way variables are supposed to work. You'll get exactly what you want, quotes intact.

If possible, use it directly instead of storing it in a variable -- 30 lines is a lot to put in one variable, and you want to avoid splitting and accidental substitution anyway. You can use a here-document to stream something into a program, variables will substitute inside it:

Code:
databaseprogram <<EOF
Select '$ENV',t1.field1,t2.field2
        from $table1 t1,$table2 t2
        where t1.field1='$var1'
        and t2.field1='$var2'
EOF


Last edited by Corona688; 08-09-2012 at 06:49 PM..
# 13  
Old 08-09-2012
Corona,

I have a query and I want to run it passing parameters at runtime. For which I tried the way I explained below and believe me that is the purpose of program.

I need to run the Query(s) in a loop.There is metadata table which has one entry for each Id. Each Id has a Query attached.Query is a field in metadata table which I get passing Id to the table.This way I get corresponding Query for an Id.
Now this Query I cannot use directly.
The parameters (clause in query) I need to put in the query at runtime.These values ( values of the variables which I need to put in Query) also I will get from the same metadata table (where I got Query for an Id from). Again I will pass the Id and will the corresponding values.These values(in my example var1,var2,table1,table2) I need to pass in Query actually.

One more complexity here. I will get two set of values for each Id.
One set is for say set A and another is for set B.
Number of parameters in both sets will be same.
The value of these parameters I need to resolve. I assign values for the query from parameter set A first , resolve query and run it.
Then I assign values for the query for parameter set B then resolve the query and run it.

Ok , obviously same query will be used for an Id no matter how many set of values we have.

This is not a standard way , but a way which I am following. Please advise how can I improve this so that I do not get those errors.

Thanks
# 14  
Old 08-09-2012
Quote:
Originally Posted by vinay4889
Corona,

I have a query and I want to run it passing parameters at runtime.
Okay.
Quote:
I need to run the Query(s) in a loop.There is metadata table which has one entry for each Id. Each Id has a Query attached.Query is a field in metadata table which I get passing Id to the table.This way I get corresponding Query for an Id.
Now this Query I cannot use directly.
The parameters (clause in query) I need to put in the query at runtime.
Okay.
Quote:
These values ( values of the variables which I need to put in Query) also I will get from the same metadata table (where I got Query for an Id from).
Now we're getting somewhere.

Instead of using shell variables, I might try using a language which has associative arrays, like awk, so values can easily be looked up and a string easily assembled without substitution anywhere you don't want it. This can be embedded in the shell script itself seamlessly, and will avoid the pitfalls of looking up variable names in the shell.

What does this metadata table look like, and how are you retrieving from it? What output would you want for given input?

Last edited by Corona688; 08-09-2012 at 07:50 PM..
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