I'm attempting to assign the Linux system command top -n1
to an array and then eliminate the first seven lines of the text that is written to a text file. When I go to print elements from my array, I get an error saying use of uninitialized value. Can someone show what I'm doing wrong when assigning my array to the system command? Thank you.
Edit: I might also add, I'm looking to delete the first seven lines by using array slicing.
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.
}
Use instead
top -bn1 | tail -n +8
No need to reinvent the wheel when the tail
command will already do what you want
You are writing the top results to a file, if you want to get them to the variable you should not do that.
Use top -bn1
instead of top -bn1 >toptmp.txt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.