PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Referencing Korn Shell Array Names as a Variable
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Referencing Korn Shell Array Names as a Variable

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2006, 21h30   #1
cradle_robber
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Referencing Korn Shell Array Names as a Variable

Greetings!

I am somewhat of a amateur at scripting in the Korn Shell. This is my
first attempt to use arrays in a script.

My problem is knowing the proper syntax to output an array element when
using variable names for both the array name and array index.

The issue is with this line of the script

"print ${$db_name[$i]}"


Here is a sample script that illustrates my problem ...

<<<< Start sample script >>>>

#!/usr/bin/ksh93

# populate names of databases
database_names="db1 db2 db3"

# four elements in each database array
typeset -i array_index=4

# populate three databases with some data
set -A db1 10 20 30 40
set -A db2 100 200 300 400
set -A db3 1000 2000 3000 4000

for db_name in $database_names

do

typeset -i i=0
echo Processing $db_name

while [[ $i -lt $array_index ]]

do

echo "DB name is $db_name, index is $i"

# This will work ...
print ${db1[0]}

# This won't, what am I doing wrong ???
print ${$db_name[$i]}

(( i = $i + 1 ))

done

done

>>> End sample script <<<<


Running the script gives the following error ...

# ksh -x test_loop

+ database_names=db1 db2 db3
+ typeset -i array_index=4
+ set -A db1 10 20 30 40
+ set -A db2 100 200 300 400
+ set -A db3 1000 2000 3000 4000
+ typeset -i i=0
+ echo Processing db1
Processing db1
+ [[ 0 -lt 4 ]]
+ echo DB name is db1, index is 0
DB name is db1, index is 0
+ print 10
10

Syntax error
test_loop[31]: ${$db_name[$i]}: 0403-011 The specified substitution is
not valid for this command.

Can anyone point me to some sample scripts or documenation that will
me with my problem?

TIA

-CR

  Réponse avec citation
Vieux 06/11/2006, 23h01   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Referencing Korn Shell Array Names as a Variable

cradle_robber wrote:
> Greetings!
>
> I am somewhat of a amateur at scripting in the Korn Shell. This is my
> first attempt to use arrays in a script.
>

<snip>
> Can anyone point me to some sample scripts or documenation that will
> me with my problem?


google "ksh arrays"

Ed.
  Réponse avec citation
Vieux 07/11/2006, 00h36   #3
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Referencing Korn Shell Array Names as a Variable

cradle_robber wrote:
> Greetings!
>
> I am somewhat of a amateur at scripting in the Korn Shell. This is my
> first attempt to use arrays in a script.
>
> My problem is knowing the proper syntax to output an array element when
> using variable names for both the array name and array index.
>
> The issue is with this line of the script
>
> "print ${$db_name[$i]}"


Are you trying a "two-phase" expansion here? Something like...

eval print \${${db_name}[$i]}


Janis

>
>
> Here is a sample script that illustrates my problem ...
>
> <<<< Start sample script >>>>
>
> #!/usr/bin/ksh93
>
> # populate names of databases
> database_names="db1 db2 db3"
>
> # four elements in each database array
> typeset -i array_index=4
>
> # populate three databases with some data
> set -A db1 10 20 30 40
> set -A db2 100 200 300 400
> set -A db3 1000 2000 3000 4000
>
> for db_name in $database_names
>
> do
>
> typeset -i i=0
> echo Processing $db_name
>
> while [[ $i -lt $array_index ]]
>
> do
>
> echo "DB name is $db_name, index is $i"
>
> # This will work ...
> print ${db1[0]}
>
> # This won't, what am I doing wrong ???
> print ${$db_name[$i]}
>
> (( i = $i + 1 ))
>
> done
>
> done
>
>
>>>>End sample script <<<<

>
>
> Running the script gives the following error ...
>
> # ksh -x test_loop
>
> + database_names=db1 db2 db3
> + typeset -i array_index=4
> + set -A db1 10 20 30 40
> + set -A db2 100 200 300 400
> + set -A db3 1000 2000 3000 4000
> + typeset -i i=0
> + echo Processing db1
> Processing db1
> + [[ 0 -lt 4 ]]
> + echo DB name is db1, index is 0
> DB name is db1, index is 0
> + print 10
> 10
>
> Syntax error
> test_loop[31]: ${$db_name[$i]}: 0403-011 The specified substitution is
> not valid for this command.
>
> Can anyone point me to some sample scripts or documenation that will
> me with my problem?
>
> TIA
>
> -CR
>

  Réponse avec citation
Vieux 07/11/2006, 21h29   #4
cradle_robber
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Referencing Korn Shell Array Names as a Variable

Janis,

Your idea worked for displaying the output from the command.

How do I store the output in a variable?

# Storing output without using variable names

$ set -A test1 10 100 1000
$ echo ${test1[1]}
100
$ testx=`echo ${test1[1]}`
$ echo $testx
100
$ db_name="test1"
$ typeset -i i=1

Works as desired, thanks!
$ eval echo "\${${db_name}[$i]}"
100


# Attempt to use variable array name and index to reference array value

$ testz=`eval echo "\${${db_name}[$i]}"`
ksh: "${${db_name}[$i]}": 0403-011 The specified substitution is not
valid for this command.

$ testz="`eval echo "\${${db_name}[$i]}"`"
ksh: "${${db_name}[$i]}": 0403-011 The specified substitution is not
valid for this command.

???

Thanks for your !

-CR



Janis Papanagnou wrote:
> cradle_robber wrote:
> > Greetings!
> >
> > I am somewhat of a amateur at scripting in the Korn Shell. This is my
> > first attempt to use arrays in a script.
> >
> > My problem is knowing the proper syntax to output an array element when
> > using variable names for both the array name and array index.
> >
> > The issue is with this line of the script
> >
> > "print ${$db_name[$i]}"

>
> Are you trying a "two-phase" expansion here? Something like...
>
> eval print \${${db_name}[$i]}
>
>
> Janis
>
> >
> >
> > Here is a sample script that illustrates my problem ...
> >
> > <<<< Start sample script >>>>
> >
> > #!/usr/bin/ksh93
> >
> > # populate names of databases
> > database_names="db1 db2 db3"
> >
> > # four elements in each database array
> > typeset -i array_index=4
> >
> > # populate three databases with some data
> > set -A db1 10 20 30 40
> > set -A db2 100 200 300 400
> > set -A db3 1000 2000 3000 4000
> >
> > for db_name in $database_names
> >
> > do
> >
> > typeset -i i=0
> > echo Processing $db_name
> >
> > while [[ $i -lt $array_index ]]
> >
> > do
> >
> > echo "DB name is $db_name, index is $i"
> >
> > # This will work ...
> > print ${db1[0]}
> >
> > # This won't, what am I doing wrong ???
> > print ${$db_name[$i]}
> >
> > (( i = $i + 1 ))
> >
> > done
> >
> > done
> >
> >
> >>>>End sample script <<<<

> >
> >
> > Running the script gives the following error ...
> >
> > # ksh -x test_loop
> >
> > + database_names=db1 db2 db3
> > + typeset -i array_index=4
> > + set -A db1 10 20 30 40
> > + set -A db2 100 200 300 400
> > + set -A db3 1000 2000 3000 4000
> > + typeset -i i=0
> > + echo Processing db1
> > Processing db1
> > + [[ 0 -lt 4 ]]
> > + echo DB name is db1, index is 0
> > DB name is db1, index is 0
> > + print 10
> > 10
> >
> > Syntax error
> > test_loop[31]: ${$db_name[$i]}: 0403-011 The specified substitution is
> > not valid for this command.
> >
> > Can anyone point me to some sample scripts or documenation that will
> > me with my problem?
> >
> > TIA
> >
> > -CR
> >


  Réponse avec citation
Vieux 07/11/2006, 21h38   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Referencing Korn Shell Array Names as a Variable

2006-11-7, 13:29(-08), cradle_robber:
[...]
> # Attempt to use variable array name and index to reference array value
>
> $ testz=`eval echo "\${${db_name}[$i]}"`
> ksh: "${${db_name}[$i]}": 0403-011 The specified substitution is not
> valid for this command.

[...]

Use $(...) instead of `...` or double the backslashes.

From ksh man page:

| Inside grave quote marks (``), \ quotes the characters \, `, and
| $. If the grave quotes occur within double quotes, then \ also
| quotes the character ".

--
Stéphane
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h33.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15191 seconds with 13 queries