Bash grep结果中的新行

Bash grep结果中的新行,bash,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-

我试图写一个简单的脚本,大致如下:

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
我也试过:

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 i in $(VBoxManage list runningvms | tr -s '\r\n' ' '); do echo $i; done

嗯,您需要将引号括在$()周围

输出:

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

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

使用更多引号
VBoxManage guestproperty获取“$i”
我尝试了你的建议,但没有成功。谢谢你的建议,但我得到了相同的结果。我还尝试了'\r'、'\n'和'\r\n'。换行符的格式是否可能出乎意料?不客气!如果您对答案满意,我希望您可以单击空心复选框将其标记为已回答。谢谢