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:
...
- 03-07-2008 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 5
"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:
The output of this code would be: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" ...
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".Code:num is: 9 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
- 03-08-2008 #2
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.
- 03-08-2008 #3Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
How about eval, here's a trivial example - I'll leave unpacking the lists to you:
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.Code:#!/bin/ksh -p LIST1="first list" LIST2="second list" read x eval echo \$$x
Easy!
- 03-10-2008 #4Just Joined!
- Join Date
- Oct 2007
- Posts
- 5
Thanks a lot guys for your suggestions. I will try them and will come back to you with my findings.

Bousmart


Reply With Quote
