Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
I use array in korn shell set -A color red blue green set -A mobile nokia se moto m=color k=mobile I am able to echo the color like below # ...
  1. #1
    Just Joined!
    Join Date
    Jul 2007
    Posts
    6

    korn shell array

    I use array in korn shell

    set -A color red blue green
    set -A mobile nokia se moto
    m=color
    k=mobile

    I am able to echo the color like below
    # echo ${color[1]}
    red

    however, I am not able to echo the variable like below
    # echo ${$m[1]}
    ksh: ${$m[1]}: bad substitution

    any idea?

  2. #2
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    You have to define m & k as arrays like you did color and mobile

    Code:
    set -A color red blue green
    set -A mobile nokia se moto
    
    set -A m ${color[*]}
    set -A k ${mobile[*]}

  3. #3
    Just Joined!
    Join Date
    Jul 2007
    Posts
    6
    Thanks for your reply

    I am not going to pass the whole array to m or k.
    what i want is use m or k as the name of array

    color is the name of first array
    set -A color red blue green

    ${array name[index]}

    # echo ${color[1]}
    red

    # echo ${$m[1]}

    instead of using color as array name directly...my script actually need to use $m to replace the array name....

    I tried many combination but cant work also
    echo "${$m[1]}"
    echo ${"$m"[1]}
    echo $"{"$m[1]"}"
    echo ${`echo $m`[1]}....

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Try this:

    eval echo \${${m}[1]}

  5. #5
    Just Joined!
    Join Date
    Jul 2007
    Posts
    6
    Quote Originally Posted by vsemaska View Post
    Try this:

    eval echo \${${m}[1]}

    it return
    server:/ >echo \${${m}[1]}
    ${color[1]}

    it only return ${color[1]} as string but not the value of the array in index one.
    suppose return red

  6. #6
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    You forgot the 'eval'

    eval echo \${${m}[1]}

  7. #7
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Yea, use bash.

  8. #8
    Just Joined!
    Join Date
    Jul 2007
    Posts
    6
    Quote Originally Posted by vsemaska View Post
    You forgot the 'eval'

    eval echo \${${m}[1]}
    Oh my god.....it really work....
    Thanks.....
    You solved my question within few mins which i spent 2 hours but couldn't find any solution...

    btw...what is eval for? evaluate? or stand for other meaning

  9. #9
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    The manpage for ksh states:

    eval command ...
    The arguments are concatenated (with spaces between them) to
    form a single string which the shell then parses and executes
    in the current environment.

    What you're doing is constructing the command you want to execute and using eval to execute it.

  10. #10
    Just Joined!
    Join Date
    Jul 2007
    Posts
    6
    Quote Originally Posted by vsemaska View Post
    The manpage for ksh states:

    eval command ...
    The arguments are concatenated (with spaces between them) to
    form a single string which the shell then parses and executes
    in the current environment.

    What you're doing is constructing the command you want to execute and using eval to execute it.
    i see...
    mean echo \${${m}[1]} will return ${color[1]} and can use eval to execute the return value like
    eval ${color[1]}

    Thanks....
    Thanks...

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...