grep结果中的新行

时间:2015-01-18 21:18:16

标签: bash

我试图写一个简单的脚本,类似于:

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...}

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

有什么建议吗?

更新以回应有关更多报价的评论。

试过这个:

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

这最后一个例子令人困惑......因为你可以看到它返回相同的2个错误,但它确实返回了正确的结果。

更新正确的解决方案

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

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

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:0)

好吧,你需要在$()附近包装引号。

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

输出:

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

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