Linux 在Perl中将系统命令分配给数组
我试图将Linux系统命令Linux 在Perl中将系统命令分配给数组,linux,perl,command,system,Linux,Perl,Command,System,我试图将Linux系统命令top-n1分配给一个数组,然后删除写入文本文件的前七行文本。当我从数组中打印元素时,我得到一个错误,说使用未初始化值。在将数组分配给系统命令时,是否有人能说明我做错了什么?多谢各位 编辑:我还可以添加,我希望通过使用数组切片删除前七行 sub processlist { my $num_of_lines = 0; my @top_command = `top -bn1 >toptmp.txt`; #opening temp f
top-n1
分配给一个数组,然后删除写入文本文件的前七行文本。当我从数组中打印元素时,我得到一个错误,说使用未初始化值。在将数组分配给系统命令时,是否有人能说明我做错了什么?多谢各位
编辑:我还可以添加,我希望通过使用数组切片删除前七行
sub processlist
{
my $num_of_lines = 0;
my @top_command = `top -bn1 >toptmp.txt`;
#opening temp file, and output file.
open(my $in, '<' , "toptmp.txt") or die "Can't read the file: $!"; #file used for initial reading
open(my $out, '>', "top.txt") or die "can't write to file: $!";
print $top_command[0], "\n";
#looping deleting first 7 lines
while(<$in>)
{
if($num_of_lines > 6) #starts writing to top.txt past line 7 (counting starts at 0)
{
print $out $_;
}
$num_of_lines++;
}
close $out;
close $in;
system("rm toptmp.txt"); #erasing tmp file.
子进程列表
{
我的$num\u行数=0;
my@top_command=`top-bn1>toptmp.txt`;
#打开临时文件和输出文件。
打开(我的$in,”改用
top -bn1 | tail -n +8
当tail
命令已经完成您想要的操作时,无需重新发明轮子您正在将最重要的结果写入一个文件中,如果您想将它们写入变量,则不应这样做
使用top-bn1
而不是top-bn1>toptmp.txt
同样,一个拥有top
的系统通常也会拥有tail
。因此添加tail
并不会使其便携性降低。