|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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}
doneThat 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 | ||
|
|
#3
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Thanks, the typeset -n or nameref works great
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
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 |
|
|