grep 结果中的新行

New line in grep result

我正在尝试编写一个简单的脚本,大致如下:

for i in $(VBoxManaged list runningvms); do
  VBoxManage guestproperty get $i "/VirtualBox/GuestInfo/Net/1/V4/IP"
done

从命令行:

VBoxManage list runningvms
"Windows 7" {1234sdfgh-sdfg-ertyu-...}
"Ubuntu 14.04 Server" {09876yhnkli-sdfg-qwert...}

问题是在 $() 中包含此命令似乎添加了换行符。

例如,上面的 for 循环产生:

VBoxManage: error: Could not find a registered machine named '"Windows'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 93 of file VBoxManageGuestProp.cpp
VBoxManage: error: Could not find a registered machine named '7"'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 93 of file VBoxManageGuestProp.cpp
Value: 192.168.8.110
VBoxManage: error: Could not find a registered machine named '"Ubuntu'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 93 of file VBoxManageGuestProp.cpp
VBoxManage: error: Could not find a registered machine named '14.04'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 93 of file VBoxManageGuestProp.cpp
VBoxManage: error: Could not find a registered machine named 'Server"'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 93 of file VBoxManageGuestProp.cpp

以下 for 循环也会产生意外结果:

for i in $(VBoxManage list runningvms); do echo $i; done
"Windows
7"
{1234sdfgh-sdfg-ertyu-...}
"Ubuntu
14.04
Server"
{09876yhnkli-sdfg-qwert...}

我已经尝试通过 grep、sed 和 tr 过滤器(和组合)传递 'VBoxManage list runningvms' 的结果,但我得到了相同的结果。

有什么建议吗?

更新以回应关于更多引述的评论。

试过这个:

for i in $(VBoxManage list runningvms); do echo "$i"; done
"Windows
7"

我也试过这个:

for i in $(VBoxManage list runningvms); do VBoxManage guestproperty get "$i" "/VirtualBox/GuestInfo/Net/1/V4/IP"; done
VBoxManage: error: Could not find a registered machine named '"Windows'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 93 of file VBoxManageGuestProp.cpp
VBoxManage: error: Could not find a registered machine named '7"'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 93 of file VBoxManageGuestProp.cpp
Value: 192.168.8.110

最后一个示例令人困惑...如您所见,return 出现了同样的 2 个错误,但随后 return 得到了正确的结果。

更新正确的解决方案

谢谢大家! Ivan X 给了我获得以下所需的最终密钥(对于可能需要它的任何其他人):

for vm in "$(VBoxManage list runningvms)"; do echo $vm; done
Ubuntu 14.04 Server {1234sdfgh-sdfg-ertyu-...}

试试这个:

for i in $(VBoxManage list runningvms | tr -s '\r\n' ' '); do echo $i; done

嗯,您需要用引号将 $() 括起来。

for i in "$(VBoxManage list runningvms)"; do echo $i; done

输出:

"Debian 7.6.0 32-bit" {82ec6db6-58b3-4b4c-aac8-d1b488fbe3e6}

或者,您可以在 运行 命令之前设置 IFS='',这样就不需要使用引号了(但您可能会在其他地方发现意想不到的效果)。