Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I'm trying to make a Korn script that returns the number of elements in a list. The user provides the list name as parameter. Look at this example: Code: ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    5

    Unhappy "List variable in other variable" problem

    Hi,

    I'm trying to make a Korn script that returns the number of elements in a list. The user provides the list name as parameter. Look at this example:

    Code:
    ...
    mylist1=(
    a b c
    d e f
    g h i
    )
    
    mylist2=(
    x
    y
    z
    )
    
    
    # For now I leave this variable commented out
    #whichlist=$1
    
    num_elements=${#mylist1[@]}
    echo "num is: $num"
    
    num_elements=${#mylist2[@]}
    echo "num is: $num"
    ...
    The output of this code would be:

    Code:
    num is: 9
    num is: 3
    This is fine. Now, I would like my script to let the user provide the list that will be read, and store it in "whichlist" to later display the element numbers of the given list. So if the user gives "mylist1", the script should respond "num is: 9", but if the user types "mylist2" the script should respond "num is: 3".

    The problem is that I don't know how to make reference of a variable in another variable. Something like... ${#${whichlist}[@]}

    For example, if $whichlist == mylist1,

    How can I retrieve the elements of the variable "mylist1" by means of the variable "whichlist". This is, how can I retrieve the elements of one variable by means of another one?

    In advance, thanks a lot for your help!

    Bousmart

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    I'm not sure this is possible in ksh or bash without using:
    • a decision structure to try to capture all possible input data (e.g. case); or
    • a script that creates and then runs another script.


    What I mean by the latter is at runtime a script replaces a dummy value in a template you've created and then runs it or sources it in.

    So, you'd have a file called 'my-template' which contains only:
    echo ${#DUMMY[@]}

    Then from the main script you'd use something like:
    sed "s/DUMMY/$whichlist/g" my-template > ready-to-run

    Finally, from the main script you'd source in ready-to-run:
    . ./ready-to-run

    The value of $whichlist could potentially change echo ${#DUMMY[@]} into echo ${#mylist1[@]}, for example.

    It ain't pretty, but you're really stretching ksh (or bash) here. Unless someone has a really elegant solution, you might need to graduate up to a more powerful language for this sort of thing.

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by anomie View Post
    ... you're really stretching ksh (or bash) here. Unless someone has a really elegant solution, you might need to graduate up to a more powerful language for this sort of thing.
    How about eval, here's a trivial example - I'll leave unpacking the lists to you:
    Code:
    #!/bin/ksh -p
    LIST1="first list"
    LIST2="second list"
    read x
    eval echo \$$x
    Run this script and type "LIST1" and you'll be rewarded with the text "first list", type "LIST2" and you'll get "second list". You need the eval to get the shell to re-parse the $$x once it's done the initial substitution of the contents of x.

    Easy!

  4. #4
    Just Joined!
    Join Date
    Oct 2007
    Posts
    5

    Wink

    Thanks a lot guys for your suggestions. I will try them and will come back to you with my findings.

    Bousmart

Posting Permissions

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