0

制作了这个脚本:

   #!/usr/local/bin/perl

#simple export from win-services with current state v. 0.1 

use Win32::Service;

 #hash-code for service reasons
my %statcodeHash = (
     '1' => 'STOPPED',
     '4' => 'RUNNING',
     '7' => 'PAUSED'
);

my %serviceHash;


 #Going to get the data from services and export

Win32::Service::GetServices("", \%serviceHash);

foreach $key(keys %serviceHash){
     my %statusHash;
     Win32::Service::GetStatus("", $serviceHash{$key}, \%statusHash);
     if ($statusHash{"CurrentState"} =~ /[1-7]/){
          print $serviceHash{"$key"} . "THE SERVISE IS " . $statcodeHash{$statusHash{"CurrentState"}} . "\n";



     }
}

我怎样才能把脚本给我的所有数据放到我硬盘上的 .txt 文件中?

这些脚本显示了我系统中的哪些服务,哪些正在运行,哪些没有。我想将此导出到 .txt 文件

谢谢

4

1 回答 1

0

而不是打印到 STDOUT,您应该将其打印到文件中以进行写入/附加。

# initialize outside foreach loop, open file for appending
open MYFILE, '>>', '/path/to/my/txtfile' or die("error writing to file $!");

# print to file
print MYFILE $serviceHash{"$key"} . "THE SERVISE IS " . $statcodeHash{$statusHash{"CurrentState"}} . "\n";

perldoc 中的更多文档

perldoc -f open
perldoc -f print
于 2013-04-26T08:32:55.640 回答