Resolving Var to its contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Resolving Var to its contents
# 1  
Old 07-20-2009
Resolving Var to its contents

Hello everyone....
I am trying to dinamically create variable names and do resolution of this vars contents.
After that is done I want to use (via a function call) the var and its contents by referring to it via the variable name.
I am having a hard time achieving this .... can you help ?

Example:

Code:
#! /bin/ksh

_func2(){
emt=test1_$1
emc=test2_$1
   echo $emt
   echo $emc

emt=$emt"_EM"
emc=$emc"_EM"
   echo $emt
   echo $emc
}

Datenow=`date +%Y%m%d%H%M`
test1_x1=/dir1/test/FileOneOne_$Datenow
test1_x2=/dir1/test/FileOneTwo_$Datenow

test2_x1=/dir1/test/FileTwoOne_$Datenow
test2_x2=/dir1/test/FileTwoTwo_$Datenow

test1_x1_EM=/dir1/test/OneOne_$Datenow
test1_x2_EM=/dir1/test/OneTwo_$Datenow

test2_x1_EM=/dir1/test/TwoOne_$Datenow
test2_x2_EM=/dir1/test/TwoTwo_$Datenow

_func2  x1
_func2  x2


When I run this I get the following output, how can I get the variable to resolve to its content?

Code:
test1_x1
test2_x1
test1_x1_EM
test2_x1_EM
test1_x2
test2_x2
test1_x2_EM
test2_x2_EM


Last edited by vgersh99; 07-20-2009 at 03:37 PM.. Reason: code tags, PLEASE!
# 2  
Old 07-20-2009
I didn't get completely, but..

Code:
emt=test1_$1
emc=test2_$1

The above can be useful only if ..

Code:
emt="$test1_$1"
emc="$test2_$1"

I suggest to please explain more about the problem.

Last edited by clx; 07-20-2009 at 03:22 PM..
# 3  
Old 07-20-2009
The code
Code:
emt=test1_$1
emc=test2_$1

is used to build a string representing the variable which I want to find the contents of.
This example code might not make any practical sense, the point of it is that I would like to be able to build a var name on the fly and be able to find its contents .....
So instead of this output:
Code:
test1_x1
test2_x1
test1_x1_EM
test2_x1_EM
test1_x2
test2_x2
test1_x2_EM
test2_x2_EM

I would like to see:
Code:
/dir1/test/FileOneOne_200907201429
/dir1/test/FileTwoOne_200907201429
/dir1/test/OneOne_200907201429
/dir1/test/TwoOne_200907201429
/dir1/test/FileOneTwo_200907201429
/dir1/test/FileTwoTwo_200907201429
/dir1/test/OneTwo_200907201429
/dir1/test/TwoTwo_200907201429

Thanks.

Last edited by gio001; 07-20-2009 at 03:47 PM..
# 4  
Old 07-20-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 5  
Old 07-20-2009
Quote:
Originally Posted by gio001
...
I would like to see:
Code:
/dir1/test/FileOneOne_200907201429
/dir1/test/FileTwoOne_200907201429
/dir1/test/OneOne_200907201429
/dir1/test/TwoOne_200907201429
/dir1/test/FileOneTwo_200907201429
/dir1/test/FileTwoTwo_200907201429
/dir1/test/OneTwo_200907201429
/dir1/test/TwoTwo_200907201429

Thanks.
There needs to be another level of evaluation to achieve that, so the function part will look like the following:

Code:
_func2(){
emt=test1_$1
emc=test2_$1
eval echo \$$emt
eval echo \$$emc

emt=$emt"_EM"
emc=$emc"_EM"
eval echo \$$emt
eval echo \$$emc
}

# 6  
Old 07-20-2009
It works well ....
Great, Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Contents deleted from /var/sadm

Hi, Somebody/somebody removed all contents /vat/sadm on Solaris 10 Sparc box. As of now server is up and running. There is no backup for this server. If we copy contents from another equivalent server, can it work ? We don't know if it will be fixed by booting form DVD also. Any suggestions? (4 Replies)
Discussion started by: solaris_1977
4 Replies

2. Shell Programming and Scripting

Csh , how to set var value into new var, in short string concatenation

i try to find way to make string concatenation in csh ( sorry this is what i have ) so i found out i can't do : set string_buff = "" foreach line("`cat $source_dir/$f`") $string_buff = string_buff $line end how can i do string concatenation? (1 Reply)
Discussion started by: umen
1 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

awk: compose regex in variable and then use its contents like $0 ~ var

A question to the awk pundits: I was thinking about composing a regex in a variable and then use its contents like $0 ~ var instead of $0 ~ /r/. Sort of indirection. Did someone run into this? Is it possible at all? (3 Replies)
Discussion started by: RudiC
3 Replies

5. Solaris

Difference between /var/log/syslog and /var/adm/messages

Hi, Is the contents in /var/log/syslog and /var/adm/messages are same?? Regards (3 Replies)
Discussion started by: vks47
3 Replies

6. Solaris

/var/adm & /var/sadm

what is the difference between tha /var/adm and /var/sadm files in solaris 10 Os please can any one respond quickly thanking you (2 Replies)
Discussion started by: wkbn86
2 Replies

7. Solaris

Lost /var/sadm/install/contents file and /var/sadm/pkg

Hello, I recently found that my /var/sadm/install/contents, ~/admin/default, /var/spool/patch and /var/spool/pkg files were empty. This broke the pkginfo, pkgchk and other package related tools. The pkgmap no longer points to where the applications have been installed. I have replaced the... (0 Replies)
Discussion started by: ronin42
0 Replies

8. Solaris

diff b/w /var/log/syslog and /var/adm/messages

hi sirs can u tell the difference between /var/log/syslogs and /var/adm/messages in my working place i am having two servers. in one servers messages file is empty and syslog file is going on increasing.. and in another servers message file is going on increasing but syslog file is... (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies

9. UNIX for Advanced & Expert Users

DNS not resolving

I have configured my solaris 9 box to obtain a IP from the DHCP server which is on windows, I have also set up the DNS with domain name and name servers under /etc/resolv.conf I can ping a ip address I just cant seem to ping hostnames, theres probaly something im not doing. I have looked through... (9 Replies)
Discussion started by: Mr Pink
9 Replies

10. UNIX for Dummies Questions & Answers

accidentally remove the contents of /var/sadm/pkg

Hi all, I carelessly remove the contents of /var/sadm/pkg. I do not have any backup of the folder. What can I do ? Thanks a lot. :eek: (11 Replies)
Discussion started by: stancwong
11 Replies
Login or Register to Ask a Question