1827635 Members
3374 Online
109966 Solutions
New Discussion

Korn Shell array syntax

 
SOLVED
Go to solution
Greg Pierro
Frequent Advisor

Korn Shell array syntax

Hello all,

If anyone can answer this it would be here. So, here is the question:

I have a script in which I set up numerous arrays. To make a long story short, I am trying to create a function (to be later used repeatedly inside of a long case statement) in which I refer to an array by using a scalar variable. Like so,

function FunctionName
{
echo "${$variable}[@]"
}

What I am trying to do is echo all the elements of the array determined by $variable. The shell is not letting me do this, no matter how I quote it, or add more braces, etc.

I know this might sound like giberish the way I am explaining it. Can anyone help?

Thanks.

greg
2 REPLIES 2
Ramkumar Devanathan
Honored Contributor
Solution

Re: Korn Shell array syntax

Greg,

Here it is - take an idea from the line str='...' -

>>
$ cat k.sh
#!/usr/bin/ksh

set -A array `cut -d: -f1 /etc/passwd`

a="array" # setting array

str='${'$a'[@]}'

eval echo ${str[@]}
<<

To reference all element in a ksh-style array, use ${array[@]} not ${array}[@].

HTH.
- ramd.
HPE Software Rocks!
Greg Pierro
Frequent Advisor

Re: Korn Shell array syntax

Thanks. Looks good. I assigned you 10 points but the drop down flipped back to 8 for some reason. I must have double clutched. The eval command was what I needed.