Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Refering to compound variables with a variable name

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-05-2012
luky55 luky55 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 13 December 2012, 9:37 AM EST
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Refering to compound variables with a variable name

Hello,

Here is my problem using KSH
I have a set of compound variables, let say cmp_var1 cmp_var2
The names of these variables are stored in an indexed array.
How can I access the subfields of these compound variables ?
I tried:

Code:
set -A cmp_varnames=(cmp_var1 cmp_var2)
for cmp in ${cmp_varnames[*]}
do
eval ${${cmp}.field}
done

That does not work, I tried a few other things, no luck.
Thanks for helping

Last edited by luky55; 12-05-2012 at 08:27 AM..
Sponsored Links
    #2  
Old Unix and Linux 12-05-2012
phunk phunk is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 28 February 2014, 3:57 PM EST
Posts: 35
Thanks: 3
Thanked 1 Time in 1 Post
Take a look at this thread.
Sponsored Links
    #3  
Old Unix and Linux 12-05-2012
gary_w's Unix or Linux Image
gary_w gary_w is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 16 July 2015, 4:39 PM EDT
Posts: 446
Thanks: 32
Thanked 96 Times in 88 Posts
I believe you are trying to do something like this. Note you need Korn shell 93::

Code:
$ cat x
#!/usr/dt/bin/dtksh

## Define Compound variables.
cmp_var1=( code=10 desc="hello" )
cmp_var2=( code=20 desc="world" )

## Create array of compound variables.
set -A cmp_varnames cmp_var1 cmp_var2

## Print the descriptions.
for cmp in ${cmp_varnames[*]}
do
  # Use a name reference instead of eval.
  typeset -n mydesc="$cmp.desc"
  print $mydesc
done

exit 0
$ ./x
hello
world
$

    #4  
Old Unix and Linux 12-05-2012
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 4 August 2015, 1:18 AM EDT
Location: /dev/ph
Posts: 4,943
Thanks: 70
Thanked 461 Times in 427 Posts
The thread referred to by Phunk will not help you.

Here is a simple working example:

Code:
#!/bin/ksh93

typeset -C cmp_var1
cmp_var1.field=date

typeset -C cmp_var2
cmp_var2.field=whoami

set -A cmp_varnames cmp_var1 cmp_var2

for cmp in ${cmp_varnames[*]}
do
   nameref my=$cmp.field
   $my
done

The Following User Says Thank You to fpmurphy For This Useful Post:
luky55 (12-05-2012)
Sponsored Links
    #5  
Old Unix and Linux 12-05-2012
luky55 luky55 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 13 December 2012, 9:37 AM EST
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks, the typeset -n or nameref works great
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compound variable in korn shell ZINGARO Shell Programming and Scripting 5 10-03-2007 10:57 AM
compound variable in korn shell ZINGARO Shell Programming and Scripting 0 08-30-2007 11:30 AM
Compound indirect variable references tkrussel UNIX for Advanced & Expert Users 5 08-21-2005 10:03 AM
Trying to use 'compound variable' in a script irina Shell Programming and Scripting 1 01-29-2004 09:33 PM
Trying to use 'compound variable' in a script neemic Shell Programming and Scripting 3 01-16-2004 06:07 AM



All times are GMT -4. The time now is 07:54 PM.