Re: Bash: command output to variable
- From: Steffen Schuler <schuler.steffenDELETETHIS@xxxxxx>
- Date: Mon, 15 May 2006 01:10:16 +0200
Steffen Schuler wrote:
------------------------- (top.pl) ---------------------------------
#!/usr/bin/env perl
# more optimal but less portable as first line:
#!/usr/bin/perl
# to quit the command you have to enter 'q' or 'Q'
# some pragmas to help error finding
use strict;
use warnings;
use diagnostics;
# perlvar: '-no_match_vars' is recommended for performance reasons
use English '-no_match_vars';
use POSIX 'strftime';
# should be installed from CPAN
use Term::ReadKey;
my $logfile = "toplog";
$English::OUTPUT_AUTOFLUSH = 1;
my $clear_string = `clear`;
open(TTY, "</dev/tty");
ReadMode "normal";
open(LOG, ">$logfile") or
die "can't open \"$logfile\": $!";
while (1) {
my $first_line = "\n";
print $clear_string;
if (open(PSOUT,
"ps -eao pcpu,pmem,comm --no-headers --sort=-pcpu |")) {
for (my $i = 0; $i < 10; ++$i) {
if (defined($_ = <PSOUT>)) {
print;
my ($F) = split;
print LOG strftime( '%T', localtime ), " $_" if $F >= 10.0;
}
}
} else {
die "can't open pipe from command: $!";
}
my $key = readKeyQuit(5);
if (defined($key) and $key =~ /q/i) {
exit 0;
}
}
close(PSOUT) or die "cant't close piped command: $!";
close(LOG) or die "can't close \"$logfile\": $!";
close(TTY) or die "can't close \"/dev/tty\": $!";
# reads key with a timeout of 5 seconds without echo
sub readKeyQuit
{
my $timeout = shift;
my $key = '';
eval {
# start alarm handler after $timeout seconds
local $SIG{ALRM} = sub { die "alarm clock restart" };
alarm $timeout;
ReadMode "raw";
# ReadKey 5, *TTY; doesn't seem to work
$key = ReadKey 0, *TTY;
ReadMode "normal";
# start alarm handler immediately
alarm 0;
};
# if the Perl syntax error message string exists and is not
# "alarm clock restart" then die
if ($EVAL_ERROR and $EVAL_ERROR !~ /alarm clock restart/) { die };
return $key;
}
---------------------- end(top.pl) -------------------------------
I didn't recognized that I exited without closing.
Better:
--------------------- begin(top.pl) ---------------------------
#!/usr/bin/env perl
# more optimal but less portable as first line:
#!/usr/bin/perl
# to quit the command you have to enter 'q' or 'Q'
# some pragmas to help error finding
use strict;
use warnings;
use diagnostics;
# perlvar: '-no_match_vars' is recommended for performance reasons
use English '-no_match_vars';
use POSIX 'strftime';
# should be installed from CPAN
use Term::ReadKey;
my $logfile = "toplog";
$English::OUTPUT_AUTOFLUSH = 1;
my $clear_string = `clear`;
open(TTY, "</dev/tty");
ReadMode "normal";
open(LOG, ">$logfile") or
die "can't open \"$logfile\": $!";
my $key;
do {
my $first_line = "\n";
print $clear_string;
if (open(PSOUT,
"ps -eao pcpu,pmem,comm --no-headers --sort=-pcpu |")) {
for (my $i = 0; $i < 10; ++$i) {
if (defined($_ = <PSOUT>)) {
print;
my ($F) = split;
print LOG strftime( '%T', localtime ), " $_" if $F >= 10.0;
}
}
} else {
die "can't open pipe from command: $!";
}
$key = readKeyQuit(5);
} until (defined($key) and $key =~ /q/i);
close(PSOUT) or die "cant't close piped command: $!";
close(LOG) or die "can't close \"$logfile\": $!";
close(TTY) or die "can't close \"/dev/tty\": $!";
# reads key with a timeout of 5 seconds without echo
sub readKeyQuit
{
my $timeout = shift;
my $key = '';
eval {
# start alarm handler after $timeout seconds
local $SIG{ALRM} = sub { die "alarm clock restart" };
alarm $timeout;
ReadMode "raw";
# ReadKey 5, *TTY; doesn't seem to work
$key = ReadKey 0, *TTY;
ReadMode "normal";
# start alarm handler immediately
alarm 0;
};
# if the Perl syntax error message string exists and is not
# "alarm clock restart" then die
if ($EVAL_ERROR and $EVAL_ERROR !~ /alarm clock restart/) { die };
return $key;
}
--------------------- end(top.pl) -----------------------------
Greetings from Munich,
Steffen
.
- References:
- Bash: command output to variable
- From: Dive
- Re: Bash: command output to variable
- From: Steffen Schuler
- Re: Bash: command output to variable
- From: John W. Krahn
- Re: Bash: command output to variable
- From: Steffen Schuler
- Bash: command output to variable
- Prev by Date: Top 10 subjects comp.unix.shell
- Next by Date: Re: Append string to each line if it matches a pattern
- Previous by thread: Re: Bash: command output to variable
- Next by thread: Vowels with an accent mark
- Index(es):
Relevant Pages
|