3

I'm writing up a bash script to list out the VM's on my host, so I can make sure they are fired up when the host machine restarts.

my issue is getting the list of the machines I need to do this for.

My full list is:

"GYO DB Clone" {1f6e2518-ed20-4d71-a974-1d4823db4a81}
"GYO LB Clone" {6ba9a751-91ed-40a4-8a90-385559f67885}
"GYO MC Clone" {8bd0d243-887f-49dd-966f-6c104e451277}
"GYO Web Clone" {1229d75d-f046-4f3e-b15a-2d6c91b1e0d4}
"GYO DB 1" {5274eb5f-d0fe-46dd-8aaf-6d23728790b1}
"GYO LB 1" {5da93ec0-2f24-47f5-936b-390f4aacae8f}
"GYO MC 1" {3914335a-8199-4d2b-861d-09b8dfd8ea19}
"GYO MC 2" {b8ae5638-2e4a-4b8c-aec4-9cdf8b830e7f}
"GYO Web 1" {9e67266f-17ae-4de9-8445-8e4a06dd9ab7}
"GYO Web 2" {645453ba-8bfb-42af-8830-143ca10a655e}
"GYO Web 3" {8851950f-7671-434b-805a-7035d126f471}
"GYO Web 4" {03d74380-3a61-486f-a61f-bc9212b081a2}

Now, I need them all to fire up... except the clones

My current code to get the list is:

sudo -H -u $VMUSER VBoxManage list vms | tr -s '\" {' '%{' | cut -d '%' -f3  | while read uuid; do
    echo "$uuid";
done

Gets me the $uuid's as it should, but now how can I mod it to exclude the clones?

And the reason I need to do it like this should be a tad obvious, but in case it's not; it's because I will not know how many vm's will need to be fired up on host reboot. I will obviously be adding/removing vm's as the needs present themselves

0

1 Answer 1

7

If sudo -H -u $VMUSER VBoxManage list vms returns this:

"GYO DB Clone" {1f6e2518-ed20-4d71-a974-1d4823db4a81}
"GYO LB Clone" {6ba9a751-91ed-40a4-8a90-385559f67885}
"GYO MC Clone" {8bd0d243-887f-49dd-966f-6c104e451277}
"GYO Web Clone" {1229d75d-f046-4f3e-b15a-2d6c91b1e0d4}
"GYO DB 1" {5274eb5f-d0fe-46dd-8aaf-6d23728790b1}
"GYO LB 1" {5da93ec0-2f24-47f5-936b-390f4aacae8f}
"GYO MC 1" {3914335a-8199-4d2b-861d-09b8dfd8ea19}
"GYO MC 2" {b8ae5638-2e4a-4b8c-aec4-9cdf8b830e7f}
"GYO Web 1" {9e67266f-17ae-4de9-8445-8e4a06dd9ab7}
"GYO Web 2" {645453ba-8bfb-42af-8830-143ca10a655e}
"GYO Web 3" {8851950f-7671-434b-805a-7035d126f471}
"GYO Web 4" {03d74380-3a61-486f-a61f-bc9212b081a2}

All you need to do is filter out any lines matching Clone. There are many ways of doing it. Here's one approach using awk:

$ sudo -H -u $VMUSER VBoxManage list vms | awk -F'[{}]' '!/Clone/{print $2}'
5274eb5f-d0fe-46dd-8aaf-6d23728790b1
5da93ec0-2f24-47f5-936b-390f4aacae8f
3914335a-8199-4d2b-861d-09b8dfd8ea19
b8ae5638-2e4a-4b8c-aec4-9cdf8b830e7f
9e67266f-17ae-4de9-8445-8e4a06dd9ab7
645453ba-8bfb-42af-8830-143ca10a655e
8851950f-7671-434b-805a-7035d126f471
03d74380-3a61-486f-a61f-bc9212b081a2

The -F[{}] sets the field separator to a character class consisting of { and }. Therefore, the 2nd field will be the machine's ID. The script then prints the second field ($2) on lines that don't match CLone (!/Clone/).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .