简体   繁体   English

从Perl脚本打印外部命令的输出

[英]Printing the output of an external command from a Perl script

My task is to print information for the user who runs this script in the terminal 我的任务是为在终端中运行此脚本的用户打印信息
The information I need to print is the information I get from writing: 我需要打印的信息是我从书面获得的信息:

parted /dev/... print

Is there any file with the same information so I can open and print it with the perl script, 是否有任何具有相同信息的文件,所以我可以使用perl脚本打开并打印它,
if not I really have no idea how to get a hold of info about which partition that is "boot" 如果不是,我真的不知道如何获得有关哪个分区“启动”的信息。
and the size of each partition 以及每个分区的大小

Feels like I've been looking all over the internet, maybe I'm just bad at searching for the 感觉就像我一直在互联网上浏览一样,也许我只是在搜索
right parameters, but I'm lost and any help is appreciated 正确的参数,但我迷路了,不胜感激

You could run the command and capture the output 您可以运行命令并捕获输出

open my $cmd, '-|', 'parted', '/dev/...', 'print' || die "Can't run command: $!";

while (<$cmd>) {
    # do something with $_, e.g.
    print;
}

close $cmd || die "Error while closing off command: $!";

That's what backticks (`) are for: 这就是反引号(`)的用途:

print `parted /dev/... print`;

print qx(parted /dev/... print);     # Another way to do it

my $output = `parted /dev/... print`;  # Save to variable

...

print $fh $output;   # Use later

See perldoc perlop for more information. 有关更多信息,请参见perldoc perlop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM