bash 使用 VBoxManage 获取正在运行的虚拟机列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7069482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a list of running VMs using VBoxManage
提问by capdragon
I want to loop through my running VM's and return only what is between quotes.
我想遍历我正在运行的 VM 并仅返回引号之间的内容。
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
回答by Mat
Warning: all of this is is risky if your VM names have shell glob characters in them, or contain spaces.
警告:如果您的 VM 名称中包含 shell glob 字符或包含空格,则所有这些都是有风险的。
You can do something like this if there is only one running VM:
如果只有一个正在运行的 VM,您可以执行以下操作:
read machine stuff <<< $(VBoxManage list runningvms)
echo "$machine"
Alternative with bash arrays (same condition):
bash 数组的替代方案(相同条件):
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
回答by jfg956
VBoxManage list runningvms | cut -d '"' -f 2 | while read machine; do
echo "$machine"
done
回答by Omriko
for one-liner fans:
对于单线风扇:
VBoxManage list runningvms | cut -d" " -f 1 | grep -oP "(?<=\").*(?=\")"
回答by Charles Duffy
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.
要在阅读时验证每一行,执行此操作的安全方法是编写一个正则表达式并用于BASH_REMATCH
从中提取匹配组。
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 的以下模拟实现(允许没有 VirtualBox 的人重现测试):
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:
请注意这种方法的优点:
- It doesn't make unfounded assumptions, such as excluding virtual machines with whitespace or quote literals in their names from support.
- It detects any line that doesn't match the expected pattern, rather than behaving in an unanticipated way in the presence of such values.
- It still behaves correctly with data that doesmatch the pattern but has unanticipated values. (For instance, a virtual machine named
*
won't have that name silently replaced with the name of a file in the current directory). - It doesn't involve tools external to the shell such as
sed
,cut
, &c., but relies purely on shell-builtin functionality -- see BashFAQ #1documenting the use ofwhile read
, and the bash-hackers' wiki on regular expression matchingdocumenting[[ $string =~ $re ]]
.
- 它不会做出毫无根据的假设,例如从支持中排除名称中包含空格或引号文字的虚拟机。
- 它检测任何与预期模式不匹配的行,而不是在存在此类值的情况下以意外方式运行。
- 它仍然与数据正确的行为是不匹配的模式,但有意外的值。(例如,一个名为的虚拟机
*
不会用当前目录中的文件名静默替换该名称)。 - 它不涉及 shell 外部的工具,例如
sed
,cut
, &c.,而是纯粹依赖于 shell 内置功能——参见BashFAQ #1记录 的使用while read
,以及bash-hackers' wiki 上的正则表达式匹配记录[[ $string =~ $re ]]
。
回答by Shizzmo
VBoxManage list runningvms | sed 's/"//g;s/ .*//'
To loop through:
循环:
for machine in `VBoxManage list runningvms | sed 's/"//g;s/ .*//'` ; do
echo $machine
done
This will break if your machine has spaces in its name.
如果您的机器名称中有空格,这将中断。