Parallel nested loop with index

Viewed 37

How to write a nested loop like the following in gnu parallel?

for h in GetHypervisors
  echo h
  for vm in GetVMs[h]
     echo vm

I have tried this example but I am not sure how to pass value to nested loop.

parallel echo {Hypervisor}; echo {VM} ::: vms {} ::: Hypervisors
1 Answers

GNU Parallel can do the product of two lists like this:

parallel echo {1}; echo {2} ::: vm1 vm2 ::: hypervisor1 hypervisor2

But I do not think you want the product, I think you want the VM-list to depend on the hypervisor-list. So you need to do:

for h in GetHypervisors; do
   for vm in $(GetVMs $h); do
     echo $h,$vm
   done
done | parallel --colsep , echo hypervisor-{1} vm-{2}

If GetVMs is slow, you can parallelize the outer loop, too.

Related