Visit Our UNIX and Linux User Community

How to evaluate a variable name on LHS of expression?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to evaluate a variable name on LHS of expression?
# 1  
Old 02-03-2014
How to evaluate a variable name on LHS of expression?

I am trying to write a simple function to select values from a database and assign them to variables. It can have any number of arguments sent into it, and I want to assign the value retrieved to a different variable name for each argument sent in. So my code looks something like this:

function getParms {
for parm
getParms parm1 parm2 parm3

I know the db2 statement works correctly. If I just run it once and hardocde the lefthand side with a variable name and then echo it out, it is selecting the proper value from the DB and assiging it correctly. But I can't get it to assign it to the parm name I send in. Essentially I need that $parm evaluated first to the parameter name and then used as the LHS of the expression. I have tried every quoting method I know of but can't get it to work. (this is Korn shell)

# 2  
Old 02-03-2014
You can't do that. You'll have to
- read $parm < someredirection
- assign to an associative array X[$parm]=...
- eval $parm=... (which brings with it all sorts of escaping needs etc.)
# 3  
Old 02-03-2014
In bash you can use printf -v like this:

function getParams {
for param in $@
   printf -v "$param" "%s" "$(./db2 +w -x "SELECT PARM_VALUE_TXT FROM ${FRMWRK_DB_SCHEMA}.JOB_PARM WHERE PARM_NM='$param' WITH UR")"

getParams parm1 parm2 parm3

# 4  
Old 02-03-2014
The OP mentioned ksh. If this is ksh93 or better there is another option called "name reference".
$ cat nameref
#! /bin/ksh

typeset -n z=a

echo $z

z=b   # z is no longer a name reference
echo now $z

typeset -n z=b
echo finally $z
exit 0
$ ./nameref
now b
finally beta

However this won't work with ksh88 which is what many unix systems have.
# 5  
Old 02-03-2014
For ksh try export

function getParams {
for param in $@
    export $param="$(db2 +w -x "SELECT PARM_VALUE_TXT FROM ${FRMWRK_DB_SCHEMA}.JOB_PARM WHERE PARM_NM='$param' WITH UR")"

getParams parm1 parm2 parm3

Last edited by Chubler_XL; 02-03-2014 at 07:57 PM..
These 3 Users Gave Thanks to Chubler_XL For This Post:
# 6  
Old 02-04-2014
Chubler_XL -

Thank you - your solution works perfectly - does exactly what I need. But I don't understand why. I thought the "export" statement just made a local variable global to where it was called from. And in searching a bit on it that's what everything I've found says it does. So I tried removing it, thinking maybe just the way you quoted the rest of the statement is what did it, but it errors if I remove "export" from the line, and if I echo that variable even inside the function it does not assign it a value. So obviously that doesn't just change the scope, it changes how the line is evaluated. For my own knowledge, can you explain what is going on that makes this work?

Thanks for your help,

# 7  
Old 02-04-2014
Yes, there is nothing special about the export command it is just another builtin command that also happens update the value of a variable. The real issue is that variable assignments are treated specially by the shell see "Simple Command Expansion" section of the bash manual.

The main bits of concern are :

1. The  words  that  the  parser has marked as variable assignments
   (those preceding the command name) and  redirections  are  saved 
   for later processing.


4. The text after the = in each variable assignment undergoes tilde
   expansion, parameter expansion, command substitution, arithmetic
   expansion, and quote removal before being assigned to the  vari-

So you see no parameter expansion is done on the text before the = (saved at point 1.).
In contrast the text following the export builtin command gets a full set of tilde, parameter, command and arithmetic expansion: giving us our loophole.

Last edited by Chubler_XL; 02-04-2014 at 03:15 PM.. Reason: clean up formatting
These 2 Users Gave Thanks to Chubler_XL For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to evaluate expression under awk?

I have to display only those subscribers which are in "unconnected state" and the date is 90 days older than today's date. Below command is used for this purpose: cat vfsubscriber_20170817.csv | sed -e 's/^"//' -e '1d' | \ nawk -F '",' '{if ( (substr($11,2,4) == 2017) && ( substr($11,2,8)... (1 Reply)
Discussion started by: dia
1 Replies

2. Shell Programming and Scripting

Evaluate Expression within awk

I want to create a conditional expression string and pass in an awk script. My script is as below... comm="\$3 == "hello"" awk -F "^T" -v command="${comm}" ' { if ( command ) { print "hye" } }' testBut the statement "if ( command )" always evaluates to true which is not... (5 Replies)
Discussion started by: Saikat123
5 Replies

3. Shell Programming and Scripting

Evaluate Variable At Runtime

Hi, I am trying to set a variable that has time the format desired. And my intention is to echo variable (instead of actual date command) everytime I like to echo date. Please take a look at below code. $NOW='' echo $NOW After 5 minutes $echo $NOW Issue here is , I am not... (2 Replies)
Discussion started by: vinay4889
2 Replies

4. Shell Programming and Scripting

Trying to execute a expression in a variable

Hi i tried to execute a below script but it is giving execution error rec=ABC,1234,55.00 Colno=2 coldel=, fd='"'$coldel'"' fprint="'"'{print$'$colno'}'"'" colsyn=`echo "echo "$rec "| awk -F"$fd $fprint` echo column syntax is $colsyn colrec=`colsyn` echo column is $colrec (5 Replies)
Discussion started by: ragu.selvaraj
5 Replies

5. Shell Programming and Scripting

Using a variable as a for loop expression

I'm writing a script to merge the xkcd webcomic tiles for comic 1110. So far, I have written about 100 lines, and instead of doing each quadrant of the image separately, I've decided to use functions to do this, repeating for every quadrant and using variables for each quadrant to make the function... (9 Replies)
Discussion started by: jbondhus
9 Replies

6. Shell Programming and Scripting

How to evaluate the value of a variable ?

How to evaluate the value of a variable ? For example: a=var $a=value !!!error happens!!! I want to evaluate var=value, how to realize it? Thanks! ---------- Post updated at 03:37 AM ---------- Previous update was at 02:22 AM ---------- I am using linux bash. a=var $a=value... (4 Replies)
Discussion started by: 915086731
4 Replies

7. Shell Programming and Scripting

Variable expression and while

hi, i'm reading a file "LISTE_FILE" like : # $LOGCOM * 5 $PRCCOM * 10 and i want to use the file with "while" and having the fields splitted into new variables for treatment : while read LINE do # Ignorer les commentaires un # en premiere position if then... (3 Replies)
Discussion started by: Nicol
3 Replies

8. Shell Programming and Scripting

Evaluate string containing shell variable names

Hello, I have this: #!/usr/bin/ksh V1=ABC str="hello 123;${V1}" eval "echo $str" i get hello 123 / ABC not found However eval works if $str variable doesn't contain a semicolumn (eg if str="hello 123~${v1}" running the eval statement above would produce (2 Replies)
Discussion started by: endorphin
2 Replies

9. Shell Programming and Scripting

Evaluate the value of a variable?

I have variables: FOO="Text" BAR="FOO" I'd like to be able to evaluate the variable named as the value of $BAR. echo $FOO Text echo $BAR FOO This is what I'd like to do: echo ${$BAR} (this won't work) Text (3 Replies)
Discussion started by: Ilja
3 Replies

10. Shell Programming and Scripting

how to get variable to re-evaluate itself?

Probably a simple one. Basically I am retrieving a number from a file - setting a variable against it and then incrementing this by 1 and using this as an entry number in a log file for messages. I need the variable to re-evalute itself each time I call it so I get the latest number in the file -... (1 Reply)
Discussion started by: frustrated1
1 Replies

Featured Tech Videos