在perl脚本中混合使用vboxmanage和bash命令

时间:2021-07-25 16:24:00

I'm using VirtualBox on Ubuntu 12.04 to run virtual machines. I'm trying to execute a VirtualBox command through the CLI, using VBoxManage. I want to capture its output in a text file, so I've written the following:

我在Ubuntu 12.04上使用VirtualBox来运行虚拟机。我正在尝试使用VBoxManage通过CLI执行VirtualBox命令。我想在文本文件中捕获它的输出,所以我写了以下内容:

use warnings;
use strict;
use File::Slurp;

my $vmname = <STDIN>;

system("vboxmanage showvminfo $vmname | > vminfo.txt");
my @vminfo = read_file('vminfo.txt');
print @vminfo;

However, nothing happens. When I open vminfo.txt it is empty. It should contain a lot of info about the VM.

然而,没有任何反应。当我打开vminfo.txt时它是空的。它应该包含很多关于VM的信息。

Is it even possible to submit commands like this? I am aware that there could be problems with having vboxmanage as the prefix. Is there a way I can escape that command and submit a bash command on the same line?

甚至可以提交这样的命令吗?我知道将vboxmanage作为前缀可能会出现问题。有没有办法可以逃避该命令并在同一行上提交bash命令?

Thanks.

1 个解决方案

#1


1  

You don't need to redirect to a file:

您无需重定向到文件:

chomp( my @vminfo = `vboxmanage showvminfo $vmname` );

#1


1  

You don't need to redirect to a file:

您无需重定向到文件:

chomp( my @vminfo = `vboxmanage showvminfo $vmname` );