如何在Perl中获取外部命令的输出?
发布时间:2020-12-15 21:19:00 所属栏目:大数据 来源:网络整理
导读:我想将Windows命令行程序(比如说powercfg -l)的输出写入使用Perl创建的文件,然后在for循环中逐行读取文件并将其分配给一个字符串。 解决方法 my $output = qx(powercfg -l);## You've got your output loaded into the $output variable. ## Still want to
我想将Windows命令行程序(比如说powercfg -l)的输出写入使用Perl创建的文件,然后在for循环中逐行读取文件并将其分配给一个字符串。
解决方法my $output = qx(powercfg -l); ## You've got your output loaded into the $output variable. ## Still want to write it to a file? open my $OUTPUT,'>','output.txt' or die "Couldn't open output.txt: $!n"; print $OUTPUT $output; close $OUTPUT ## Now you can loop through each line and ## parse the $line variable to extract the info you are looking for. foreach my $line (split /[rn]+/,$output) { ## Regular expression magic to grab what you want } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |