0

So I am making a script that allows you to input a number, export it, and then import it and use it in a loop. Here's what I mean:

read NumberOfVMs "How many do you want? "
echo $NumberOfVMs > /variables/NumberOfVMs

Then later;

while [ $NumberOfVMs -gt 0 ];do
    # This is the loop I use to repeat the effects I want. 
    # This method works fine for me.
    NumberOfVMs=$((NumberOfVMs-1))
done

However, what gets me is that I need to use variables that are listed by number (based on what $NumberOfVMs is equal to. I also want to zero pad the number to four 0s. I know I can zero pad by doing $(printf %04g $NumberOfVMs).

For example, I want to be able to make 3 variables (respectively with 0001, 0002, and 0003 added to the end of the variable name) when asking a question. I am currently doing it like this

while [ $NumberOfVMs -gt 0 ];do          
    read -p "Enter percentage of RAM to allot GuestOS (1-99): " percentram$(printf %04g $NumberOfVMs)
    NumberOfVMs=$((NumberOfVMs-1))
done

and, while I do believe (I might be wrong) that say, percentram0001, is being written - I cannot figure out how to use it dynamically when using the variable as $percentram$(printf %04g $NumberOfVMs) would not equal percentram0001 but rather equal the output of percentram with 0001 added to it.

Please, if you could help me out I'd love you forever.

Crutchcorn
  • 199
  • 5
  • 14

1 Answers1

2

You can use eval hack:

NumberOfVMs=10
read -p "Enter percentage of RAM to allot GuestOS (1-99): " count
eval "percent$(printf %04g $NumberOfVMs)=$count"
echo $percent0010
peter
  • 84
  • 2
  • Wait.... But then how do I read it again as being dynamic? I now know how to set $percent0010, but then how do I read $percent$(printf %04g $NumberOfVMs) (in this case being percent0010) based on how many VMs there are? – Crutchcorn Feb 10 '15 at 05:15
  • the same hack works :) eval "somevar=\$percent$(printf %04g $NumberOfVMs)" – peter Feb 10 '15 at 06:23
  • having said that, this is very hacky, you're much better off using arrays (either regular, or associative): http://tldp.org/LDP/abs/html/arrays.html or http://www.linuxjournal.com/content/bash-associative-arrays. Syntax is a bit hairy, I almost always have to look it up... – peter Feb 10 '15 at 06:25
  • Good nough. :) Thanks – Crutchcorn Feb 10 '15 at 07:07
  • One last question - I have a section where I have to export the commands by using `echo $percent > /variables/percent` to export to a file and export `percent='cat /variables/percent'` to import them again... How can I use this hack to implement this? I know I use the loop mentioned in the question... But now how do I use this method to know what variable I am talking about? – Crutchcorn Feb 10 '15 at 08:31
  • again, the same thing. `percent='cat /variables/percent'` `eval percent$(printf %04g $NumberOfVMs)=$percent`. If I understood your question correctly. – peter Feb 12 '15 at 03:10
  • Not quite as I understand it... I want to be able to have a loop that exports percent0001, percent0002, etc. – Crutchcorn Feb 17 '15 at 04:52