|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 > |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 > > |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|