Monday, March 22, 2010

Using "for" loop in automating repetitive tasks

The "for" loop is a very powerful tool in automating repetitive tasks. A good example would be cloning virtual hard drives in VirtualBox (I use this for my development and RnD as my sandbox, Thank you SUN!). Below shows the basic usage of the for loop on the bash cli.

In this example, I would like to clone my VirtualBox hard disk from one of my SuSE 10 servers hard disk, as I don't want to do repetitive installation. But instead, I will just modify the settings of my new "cloned" harddrive(s) tp the settings that will suite my new VirtualBox machine to my liking (or yours).

for i in {0..5} ; do VBoxManage clonehd SuSE.10.LVS.00.vdi SuSE.10.MYSQL.0$i.vdi ; done

The command above will iterate 'i' number of times as defined inside the range {0..5} which in this case will be 6 (0,1,2,3,4,5) that is.

No comments:

Post a Comment