It sounds like you are trying to rewrite ls | more
. Do you really need to do this?
The program below opens the file temp.txt
for writing. It then usesqx
(the equivalent of backticks) to get the output from a command and write it to the file.
The same file is then reopened for reading, and twenty lines at a time are sent to STDOUT
.
To allow character IO from the keyboard the read mode is set to 3 and ReadKey
is called to fetch the next keypress. These calls require the Term::ReadKey
module to be specified in a use
statement at the top of the program. It is a core module and so shouldn’t need installing
use strict;
use warnings;
use Term::ReadKey;
open my $fh, '>', 'temp.txt' or die $!;
print $fh qx(ls /bin/usr/account/users/);
open $fh, '<', 'temp.txt' or die $!;
ReadMode(3);
my $n = 20;
while (<$fh>) {
print;
if (--$n == 0) {
print "-- More --";
ReadKey(0);
$n += 20;
}
}
1
solved A Perl script that internally calls a Linux command and saves the output to a file? [closed]