Operating System - HP-UX
1821244 Members
2851 Online
109632 Solutions
New Discussion юеВ

juggling with variables and arrays under the POSIX shell

 
SOLVED
Go to solution
Francis No├лl
Regular Advisor

juggling with variables and arrays under the POSIX shell

Greetings forum users

I am having trouble concatenating variables into a useful command. I'm working with (pseudo?)arrays under /sbin/sh.

Behold !

lhpdev0(/)# echo $SHELL
/sbin/sh
lhpdev0(/)# echo $i
35
lhpdev0(/)# echo $bd35
lhpdev0.avdl.com

$i is an iteration counter
$bd35 is the first index of an array

I get stuck trying to get a value out of bd$i.
I have tried fooling around whith braces, that didnt help.

lhpdev0(/)# echo $bd$i
sh: bd: Parameter not set.
lhpdev0(/)# echo $bd${i}
sh: bd: Parameter not set.
lhpdev0(/)# echo ${bd${i}}
sh: ${bd${i}}: The specified substitution is not valid for this command.

So how would I work the shell's magic to obtain say the first index of $bd[0-35], using the iteration counter $i ?

TIA !
6 REPLIES 6
Francis No├лl
Regular Advisor

Re: juggling with variables and arrays under the POSIX shell

Some clarification might be needed...

Here is some more background.

I have a number of arrays ( bd0 through bd?? ) and at some point in my script I will need to get the value of an index for each of them.

I planned on doing a while loop, using good o'l i as the iteration counter, and getting the index I need for each array up to ?? with something like this :

Note "bds" is an array containing the list of bd arrays :)

i=0
while [ $i -lt ${#bds[*]} ]
do
echo ${bd${i}[0]} <---Need help here!
((i=i+1))
done

Hope this helps you guys understand what my goal is.
Court Campbell
Honored Contributor

Re: juggling with variables and arrays under the POSIX shell

maybe this will help you:

set -A bd red green blue purple

i=0

while [ $i -lt ${#bd[*]} ]
do
echo ${bd[$i]}
i=$(($i +1))
done
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Francis No├лl
Regular Advisor

Re: juggling with variables and arrays under the POSIX shell

Court your reply is correct and functionnal but you are using $i to cycle through the indexes of one array.

I am using $i to cycle through a list of arrays, retrieving a fixed index.

Keep 'em coming :)
Sandman!
Honored Contributor
Solution

Re: juggling with variables and arrays under the POSIX shell

>lhpdev0(/)# echo $bd$i
>sh: bd: Parameter not set.

Change the above echo stmt as follows:

eval echo \$bd$i

# let i=35
# bd35="lhpdev0.avdl.com"

# eval echo \$bd$i
lhpdev0.avdl.com

~hope it helps
Francis No├лl
Regular Advisor

Re: juggling with variables and arrays under the POSIX shell

Bingo !

eval does the trick


Thanks guys !
Court Campbell
Honored Contributor

Re: juggling with variables and arrays under the POSIX shell

Francis,

You aren't using arrays. As you already stated

>with (pseudo?)arrays

My point was to use actual arrays (not directly implied).
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"