Assign variable name through loop


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign variable name through loop
# 1  
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:

Code:
#!/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
# 2  
Code:
for i in {1..8}; do
   eval var$i='"some command"'
done

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

Code:
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.

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

does not work.

any ideas?
# 4  
can you provide the ouputput of $blinfo2 and the expected output
This User Gave Thanks to itkamaraj For This Post:
# 5  
Hi

This is the output from $blinfo2

Code:
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.

Code:
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
# 6  
Like this may be:
Code:
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.
This User Gave Thanks to elixir_sinari For This Post:
# 7  
Thanks a lot guys for the help! Now my script works as it should.

You are awesome!
Tobbe
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #414
Difficulty: Easy
JavaScript is a high-level, interpreted scripting language that conforms to the ECMAScript specification.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. Shell Programming and Scripting

Assign Values to a Variable in While Loop and Update the File

Hello, Could anyone please help me with Assigning a value to variable and then updating the value in the original file IFS='|' while read -r Serial_ID JOB_NAME STATUS do if then echo "Perform Fuctions" ???Assign STATUS to COMPLETED and Update File??? done <File (7 Replies)
Discussion started by: infernalhell
7 Replies

4. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

5. Shell Programming and Scripting

Do While Loop + Read From File + assign line to a variable

Hello, I am using below code for reading from a file and assigning the values to a variable , but it is loosing the value after the loop , please suggest to retain the value of the variable after the loop , while IFS=: read -r line do set $dsc=$line echo 'printing line variable ' $line... (1 Reply)
Discussion started by: ParthThakkar
1 Replies

6. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

7. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

8. Shell Programming and Scripting

Re-assign variable's value through which FOR LOOP loops

Hi, I've a requirement where I want to re-assign the value in the variable through which FOR LOOP loops. For e.g. Snippet of code --------------- for i in $var do echo $i >> $tempFile var=`echo $another_var | awk -F" " '{print $1}'` done I am re-assigning var so... (2 Replies)
Discussion started by: dips_ag
2 Replies

9. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

10. Shell Programming and Scripting

Is it possible to re-assign the variable form the loop?

Good day, everyone! I've got a small problem with re-assigning the variable form the loop. What I'm trying to do is: #!/bin/bash/ VAR1="AAA" VAR2="BBB" VAR3="CCC" for WORD in VAR1 VAR2 VAR3; do $WORD="DDD" echo $WORD done :o That's the output and error messages: -bash:... (2 Replies)
Discussion started by: Nafanja
2 Replies

Featured Tech Videos