PDA

View Full Version : [bash] How to combine these arrays?



bttb
August 28th, 2008, 06:09 PM
Hi all,

I have two arrays in my script which turned out to be quite handy. The first holds variable names, the second options:


array[0]=THREADS OPTION[0]=4
array[1]=COLOR OPTION[1]=GREEN
array[2]=BOLDFACE OPTION[2]=YES
... ...


I'd like to go from there to here:


THREADS=4
COLOR=GREEN
BOLDFACE=YES
...

$ echo $COLOR
GREEN

Anybody knows how to achieve this without using eval (${!VAR} is fine)?

Kind regards!

humberto.cruz
August 28th, 2008, 09:16 PM
why can't you use eval ?

c=0;while [ $c -lt ${#names[@]} ]; do eval ${names[$c]}=${options[$c]};c=$((c+1)); done

[]s
Humberto

bttb
August 30th, 2008, 01:45 PM
This works:



#!/bin/bash

array[0]=THREADS
array[1]=COLOR
array[2]=BOLDFACE

OPTION[0]=4
OPTION[1]=GREEN
OPTION[2]=YES

for i in 0 1 2; do
declare ${array[i]}=${OPTION[i]}
done

echo \$COLOR = $COLOR
echo \$THREADS = $THREADS
echo \$BOLDFACE = $BOLDFACE

exit