Assign variable name through loop

Hi

As bash does not support multidimensional arrays (?), I need some help with a problem. What I want to do is to assign variable names containing a counter in a loop .

what I want to do is basically something like this:

#!/bin/bash

for i in {1..8}; do
   var$i = "some command"
done

echo $var1
echo $var2
....
echo $var8

Can this be done?

Regards,
Tobbe

for i in {1..8}; do
   eval var$i='"some command"'
done

Thanks for that, but maybe I should have been more clear by including the real line:

cpuarray$i+=$(echo $blinfo2 | awk '{for(j=38;j<=203;j+=11) print $j}')

Basically, every 11'th value in the string $blinfo2 (starting at position 38) should be added to a new variable every time the loop is performed. This should result in variables named cpuarray1, cpuarray2 etc with 16 values in each.

I can't get it to take the ':s, i.e.

eval cpuarray$i+='$(echo $blinfo2 | awk '{for(j=38;j<=203;j+=11) print $j}')'

does not work.

any ideas?

can you provide the ouputput of $blinfo2 and the expected output

1 Like

Hi

This is the output from $blinfo2

Linux 3.0.13-0.27-default (gotc101) 01/10/12 _x86_64_ 09:26:00 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle 09:26:00 all 8.23 0.00 0.92 0.01 0.00 0.00 0.00 0.00 90.83 09:26:00 0 5.85 0.00 2.41 0.07 0.00 0.02 0.00 0.00 91.64 09:26:00 1 10.26 0.00 4.49 0.02 0.00 0.02 0.00 0.00 85.21 09:26:00 2 8.16 0.00 0.57 0.02 0.00 0.00 0.00 0.00 91.26 09:26:00 3 8.56 0.00 0.58 0.02 0.00 0.00 0.00 0.00 90.84 09:26:00 4 8.16 0.00 0.49 0.01 0.00 0.00 0.00 0.00 91.34 09:26:00 5 8.59 0.00 0.79 0.01 0.00 0.00 0.00 0.00 90.61 09:26:00 6 8.21 0.00 0.81 0.01 0.00 0.00 0.00 0.00 90.97 09:26:00 7 8.37 0.00 0.53 0.01 0.00 0.00 0.00 0.00 91.09 09:26:00 8 8.06 0.00 0.48 0.01 0.00 0.00 0.00 0.00 91.45 09:26:00 9 8.40 0.00 0.49 0.01 0.00 0.00 0.00 0.00 91.10 09:26:00 10 7.99 0.00 0.50 0.01 0.00 0.00 0.00 0.00 91.50 09:26:00 11 8.35 0.00 0.53 0.01 0.00 0.00 0.00 0.00 91.12 09:26:00 12 8.02 0.00 0.50 0.01 0.00 0.00 0.00 0.00 91.48 09:26:00 13 8.44 0.00 0.49 0.01 0.00 0.00 0.00 0.00 91.07 09:26:00 14 7.95 0.00 0.52 0.01 0.00 0.00 0.00 0.00 91.52 09:26:00 15 8.35 0.00 0.57 0.01 0.00 0.00 0.00 0.00 91.08

What I want to be put in arrays is the 11'th value in that string, starting from the 38'th value (bold).

This shall then be done 14 times (14 blades in the machine). what I want the script to do is to store the idle CPU% in 14 vectors (16 values = 16 cores), one for each blade.

echo "Blade $i:"
rline="ssh gotc10$i mpstat -P ALL | awk '{print $3}'"
eval blinfo2=\$\($rline\)

eval cpuarray$i+=$(echo $blinfo2 | awk '{for(j=38;j<=203;j+=11) print $j}')

echo $cpuarray$i

Thanks,
Tobbe

Like this may be:

for i in {1..14}; do
 echo "Blade $i:"
 rline="ssh gotc10$i mpstat -P ALL | awk '{print $3}'"
 blinfo2=$(eval $rline)
 eval cpuarray$i+='($(echo $blinfo2 | awk "{for(j=38;j<=203;j+=11) print \$j}"))'
 eval echo '"${cpuarray'$i'[@]}"'
done

cpuarray1, cpuarray2,....,cpuarray14 will be indexed arrays containing the 16 values for each of the 14 blade servers.

1 Like

Thanks a lot guys for the help! Now my script works as it should.

You are awesome!
Tobbe