I want to loop through my running VM's and return only what is between quotes.
So this command:
VBoxManage list runningvms
returns:
"UbuntuServer" {7ef01f8d-a7d5-4405-af42-94d85f999dff}
And I only want it to return:
UbuntuServer
This is what i have so far (fail):
#!/bin/bash
for machine in `cat VBoxManage list runningvms`; do
echo "$machine"
done
exit
To see all VMs on the local Hyper-V host, you should run the Get-VM cmdlet. On the PowerShell screen, you can see the list of available VMs, including their name, state, CPU usage, memory assigned, uptime, status, and version.
You can use The showvminfo command for show information about a particular virtual machine. This is the same information as VBoxManage list vms would show for all virtual machines.
You can easily list all running Virtual machines from a Linux shell prompt without accessing GUI. This is useful to scripts or to get status for any VM. => -h 'https://vmserver.example.com:8333/sdk' : VMWare server hostname. This can be local or remote server.
vms: Lists all virtual machines currently registered with Oracle VM VirtualBox. By default this displays a compact list with each VM's name and UUID. If you also specify --long or -l , this will be a detailed list as with the showvminfo command, see Section 7.5, “VBoxManage showvminfo”.
VBoxManage list runningvms | cut -d '"' -f 2 | while read machine; do
echo "$machine"
done
Warning: all of this is is risky if your VM names have shell glob characters in them, or contain spaces.
You can do something like this if there is only one running VM:
read machine stuff <<< $(VBoxManage list runningvms)
echo "$machine"
Alternative with bash arrays (same condition):
vbm=($(VBoxManage list runningvms))
echo "${vbm[0]}"
If that program returns more than one line, a more classic approach would be:
for machine in $(VBoxManage list runningvms|cut -d" " -f 1); do
echo "$machine"
done
for one-liner fans:
VBoxManage list runningvms | cut -d" " -f 1 | grep -oP "(?<=\").*(?=\")"
To validate each line as you read it, the safe way to do this is to write a regular expression and use BASH_REMATCH
to extract match groups from it.
With the following code:
re='^"(.*)" [{]([0-9a-f-]+)[}]$'
while read -r line; do
if [[ $line =~ $re ]]; then
name=${BASH_REMATCH[1]}; uuid=${BASH_REMATCH[2]}
echo "Found VM with name $name and uuid $uuid" >&2
else
echo "ERROR: Could not parse line: $line" >&2
fi
done < <(VBoxManage list runningvms)
...and the following mock implementation of VBoxManage (to allow folks without VirtualBox to reproduce the test):
VBoxManage() { printf '%s\n' '"UbuntuServer" {7ef01f8d-a7d5-4405-af42-94d85f999dff}'; }
...output is as follows:
Found VM with name UbuntuServer and uuid 7ef01f8d-a7d5-4405-af42-94d85f999dff
Note advantages of this approach:
*
won't have that name silently replaced with the name of a file in the current directory).sed
, cut
, &c., but relies purely on shell-builtin functionality -- see BashFAQ #1 documenting the use of while read
, and the bash-hackers' wiki on regular expression matching documenting [[ $string =~ $re ]]
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With