For loop with one variable as the name of other

Hello all,

I find it hard to explain what I need so I will post the code

OVZINCLUDE="16810 16811 1689"
PLUS_16810="test"
PLUS_16811="test"
for VPS in $OVZINCLUDE
        do

          echo "Dumping VPSes: $OVZINCLUDE "
           vzdump --compress --snapshot ${PLUS_$VPS} $VPS
done

The idea is when for parses 16810 to use PLUS_16810 as vzdump argument, same for 16811 and etc.

The code above gives bad substitution error.
I know I can use eval for the naming of the variable as something_$VARIABLE, but how to use it in reverse?

p.s. That is in
bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)

Would this do it for you:

OVZINCLUDE="16810 16811 1689"
for VPS in $OVZINCLUDE
do
   echo "Dumping VPSes: $OVZINCLUDE "
    mPlus='PLUS_'${OVZINCLUDE}
   vzdump --compress --snapshot ${mPlus}
done
1 Like

Yes! With little correction

OVZINCLUDE="16810 16811 1689"
PLUS_16810="help"
for VPS in $OVZINCLUDE
do
   echo "Dumping VPSes: $OVZINCLUDE "
    mPlus='PLUS_'${VPS}
   vzdump --compress --snapshot ${!mPlus}
done

That way it purses the variable only when for is processing PLUS_$VPS

Thanks a lot!