Home > Archive > PerlTk > April 2006 > capture unix cmd outputs
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
capture unix cmd outputs
|
|
| Shankar 2006-04-25, 7:05 pm |
| Hello. I am trying to print the output of unix command typed in a
LabEntry in the Text widget upon button click. The code is modified
version of the one avialable in perl/tk FAQ website. But it gives "no
such file or directory" error. Any help is appreciated.
Thnx in advance.
S
#!/usr/local/bin/perl -w
use Tk;
require Tk::BrowseEntry;
# main window
my $mw = MainWindow->new(-title => "my perl/tk");
my $cFrame = $mw->Frame(
-relief => 'ridge',
-borderwidth => 2,
)->pack(-side => 'top',
-fill => 'x',
-anchor => 'e',
-expand => 1);
my $c2Frame = $cFrame->Frame(
)->pack(-side => 'top',
-fill => 'x',
-anchor => 'e',
-expand => 1);
my $cmd = "ls";
$c2Frame->LabEntry(-textvariable => \$cmd,
-label => "Cmd: ", -labelPack => [-side =>
'left'],
)->pack(-side => 'left', -fill => 'x', -anchor => 'e', -
expand => 1);
my $dFrame = $mw->Frame(
-relief => 'ridge',
-borderwidth => 2,
)->pack(-side => 'top',
-fill => 'both',
-expand => 1);
my $d1Frame = $dFrame->Frame(
)->pack(-side => 'top',
-fill => 'x',
-expand => 1);
my $d2Frame = $dFrame->Frame(
)->pack(-side => 'top',
-fill => 'both',
-expand => 1);
$d1Frame->Label(-text => "Log:")
->pack(-side => 'left',
-anchor => 'w',
-expand => 0);
$d1Frame->Label(-text => " ")
->pack(-side => 'left',
-fill => 'x',
-anchor => 'w',
-expand => 1);
my $cmdBtn = $d1Frame->Button(-text => "Run", -command=>
\&printCmd)
->pack(-side => 'left',
-anchor => 'e',
-expand => 0);
my $t = $d2Frame->Text(-wrap => 'word'); # used -state =>
'normal' | 'disabled' to affect editing!!
$t->pack(-side => 'bottom', -fill => 'both', -expand => 1);
$t->menu(undef); # stop the menu
MainLoop;
sub printCmd {
print "Here! ", $cmd, "\n";
open(H, '$cmd') or die "Nope: $!";
select H ;
# $| = 1 ;
select STDOUT ;
# $mw->fileevent(H, 'readable', [\&fill_text_widget, $t]);
$mw->fileevent(H, [\&fill_text_widget]);
sub fill_text_widget {
my($widget) = @ARG;
$ARG = <H>;
$t->insert('end', $ARG);
$t->yview('end');
} # end fill_text_widget
#my @files = `ls -l | grep -v \"^d\" | awk '{print \$9\}'`;
#foreach $file (@files)
#{
#print "$file ";
#}
}
1;
__END__
| |
| zentara 2006-04-25, 7:05 pm |
| On Mon, 24 Apr 2006 10:32:24 +0000 (UTC), Shankar <s022930@dtu.dk>
wrote:
>Hello. I am trying to print the output of unix command typed in a
>LabEntry in the Text widget upon button click. The code is modified
>version of the one avialable in perl/tk FAQ website. But it gives "no
>such file or directory" error. Any help is appreciated.
You have a couple of small errors.
>Thnx in advance.
>S
>
>
>
>#!/usr/local/bin/perl -w
>use Tk;
>require Tk::BrowseEntry;
>
># main window
>my $mw = MainWindow->new(-title => "my perl/tk");
>
> my $cFrame = $mw->Frame(
> -relief => 'ridge',
> -borderwidth => 2,
> )->pack(-side => 'top',
> -fill => 'x',
> -anchor => 'e',
> -expand => 1);
> my $c2Frame = $cFrame->Frame(
> )->pack(-side => 'top',
> -fill => 'x',
> -anchor => 'e',
> -expand => 1);
> my $cmd = "ls";
> $c2Frame->LabEntry(-textvariable => \$cmd,
> -label => "Cmd: ", -labelPack => [-side =>
>'left'],
> )->pack(-side => 'left', -fill => 'x', -anchor => 'e', -
>expand => 1);
>
> my $dFrame = $mw->Frame(
> -relief => 'ridge',
> -borderwidth => 2,
> )->pack(-side => 'top',
> -fill => 'both',
> -expand => 1);
> my $d1Frame = $dFrame->Frame(
> )->pack(-side => 'top',
> -fill => 'x',
> -expand => 1);
> my $d2Frame = $dFrame->Frame(
> )->pack(-side => 'top',
> -fill => 'both',
> -expand => 1);
> $d1Frame->Label(-text => "Log:")
> ->pack(-side => 'left',
> -anchor => 'w',
> -expand => 0);
> $d1Frame->Label(-text => " ")
> ->pack(-side => 'left',
> -fill => 'x',
> -anchor => 'w',
> -expand => 1);
> my $cmdBtn = $d1Frame->Button(-text => "Run", -command=>
>\&printCmd)
> ->pack(-side => 'left',
> -anchor => 'e',
> -expand => 0);
> my $t = $d2Frame->Text(-wrap => 'word'); # used -state =>
>'normal' | 'disabled' to affect editing!!
> $t->pack(-side => 'bottom', -fill => 'both', -expand => 1);
> $t->menu(undef); # stop the menu
>
>
>MainLoop;
>
>
>sub printCmd {
>
> print "Here! ", $cmd, "\n";
>
You need a piped open here. Where's the pipe?
> open(H, '$cmd') or die "Nope: $!";
open(H, "$cmd |" ) or die "Nope: $!";
> select H ;
># $| = 1 ;
> select STDOUT ;
This first one is good.
># $mw->fileevent(H, 'readable', [\&fill_text_widget, $t]);
> $mw->fileevent(H, [\&fill_text_widget]);
>
> sub fill_text_widget {
It's @_ NOT @ARG
> my($widget) = @ARG;
my ($widget) = @_;
$ARG will only read 1 line
you need @ARG
> $ARG = <H>;
> $t->insert('end', $ARG);
> $t->yview('end');
>
@ARG = <H>;
$widget->insert('end',@ARG);
$widget->see('end')
> } # end fill_text_widget
>
>1;
>__END__
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
| |
| Shankar 2006-04-25, 7:05 pm |
| Hello, I applied the suggested changes. But, now I get the following
error:
========
Here! ls
c:\cygwin\bin\perl.exe (3944): *** unable to remap c:\cygwin\bin
\cygz.dll to same address as parent(0xE00000) != 0xE10000
19 [main] perl 2016 fork_parent: child 3944 died waiting for dll
loading
Background Error: panic: MUTEX_LOCK (45) [op.c:354] at
/usr/lib/perl5/vendor_perl/5.8/cygwin/Tk.pm line 406.
panic: MUTEX_LOCK (45) [op.c:354].
========
Any advice?
S
zentara <zentara@highstream.net> wrote in
news:5nbp42lkf52rbvebu26ti4jrnrdpc9hegf@
4ax.com:
> On Mon, 24 Apr 2006 10:32:24 +0000 (UTC), Shankar <s022930@dtu.dk>
> wrote:
>
>
> You have a couple of small errors.
>
>
> You need a piped open here. Where's the pipe?
> open(H, "$cmd |" ) or die "Nope: $!";
>
>
> This first one is good.
> It's @_ NOT @ARG
> my ($widget) = @_;
>
> $ARG will only read 1 line
> you need @ARG
>
> @ARG = <H>;
> $widget->insert('end',@ARG);
> $widget->see('end')
>
>
>
| |
| Ala Qumsieh 2006-04-25, 7:05 pm |
| Shankar wrote:
> open(H, '$cmd') or die "Nope: $!";
those should be back quotes, not regular single quotes:
open H, `$cmd` or die "Nope: $!\n";
--Ala
| |
| Shankar 2006-04-25, 7:05 pm |
| Ala Qumsieh <notvalid@email.com> wrote in news:AN63g.51512$_S7.41977
@newssvr14.news.prodigy.com:
> those should be back quotes, not regular single quotes:
>
> open H, `$cmd` or die "Nope: $!\n";
>
Fixed. But, the issue is still not resolved. The result is strange MUTEX
error (see previous post).
S
| |
| Shankar 2006-04-25, 7:05 pm |
| Hello. Sorry to ask again, but now urgently need the solution. (Project
deadline moved up!)
The issue seems to be with the line:
$mw->fileevent(H, 'readable', [\&fill_text_widget, $t]);
I can see the command being passed to the native OS and the result being
printed in the DOS window, but that output text is no captured and
displayed in the Text widget. This is a simple example and there should
not be this complication. Grateful for any help.
Cheers,
S
The subroutine of interest (main code in previous post):
sub printCmd {
open(H, "$cmd |") or die "Nope: $!\n";
select H;
select STDOUT;
$mw->fileevent(H, 'readable', [\&fill_text_widget, $t]);
print "Here2a! \n";
sub fill_text_widget {
print "Here3! \n"; # does not print, why ??
my($widget) = @_;
@ARG = <H>;
print "Output: ", @ARG;
$widget->insert('end', @ARG);
$widget->see('end');
} # end fill_text_widget
close(H);
}
| |
| zentara 2006-04-25, 7:05 pm |
| On Tue, 25 Apr 2006 09:39:42 +0000 (UTC), Shankar <s022930@dtu.dk>
wrote:
>Hello. Sorry to ask again, but now urgently need the solution. (Project
>deadline moved up!)
>
>The issue seems to be with the line:
>
>$mw->fileevent(H, 'readable', [\&fill_text_widget, $t]);
>
>I can see the command being passed to the native OS and the result being
>printed in the DOS window, but that output text is no captured and
>displayed in the Text widget. This is a simple example and there should
>not be this complication. Grateful for any help.
>
>Cheers,
I don't use Windows, but this problem has been reported before.
Namely, you can't count on Tk::fileevent to work on windows.
See http://perlmonks.org?node_id=470827
There are alternatives that you can use in the above link.
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
| |
| Shankar 2006-04-25, 7:05 pm |
| zentara <zentara@highstream.net> wrote in
> I don't use Windows, but this problem has been reported before.
> Namely, you can't count on Tk::fileevent to work on windows.
>
> See http://perlmonks.org?node_id=470827
>
How about in cygwin? I check out the website now.
| |
| Jack D 2006-04-25, 7:05 pm |
|
"Shankar" <s022930@dtu.dk> wrote in message
news:e2kqou$ek2$1@news.net.uni-c.dk...
> Hello. Sorry to ask again, but now urgently need the solution. (Project
> deadline moved up!)
>
> The issue seems to be with the line:
>
> $mw->fileevent(H, 'readable', [\&fill_text_widget, $t]);
>
> I can see the command being passed to the native OS and the result being
> printed in the DOS window, but that output text is no captured and
> displayed in the Text widget. This is a simple example and there should
> not be this complication. Grateful for any help.
>
You haven't given enough information on "how long will this command take?"
If it is less than a few seconds, then I would suggest just let your GUI
freeze for that bit of time and use backticks to get your output. I
certainly wouldn't go through all the hassle you have so far; if it is a
fast running command. Something as simple as the following code might
suffice for your needs:
###############################
# Try typing in a smple command like
# "dir" or "perl -v" or "hostname"
# without the quotes of course.
use Tk;
use strict;
my $cmd = 'dir';
my $mw=tkinit;
my $entry = $mw->Entry(-textvariable=>\$cmd)->pack;
$entry->bind('<Return>'=>\&runcmd);
my $button = $mw->Button(-text=>'Run Command',-command=>\&runcmd)->pack;
my $text=$mw->Scrolled('Text',-scrollbars=>'se')->pack;
MainLoop;
sub runcmd {
$mw->Busy(-recurse=>1);
my $stuff = `$cmd`;
$text->insert('end',$stuff);
$text->see('end');
$mw->Unbusy;
}
__END__
###############################
Jack
| |
| Shankar 2006-04-26, 8:01 am |
| >>
> You haven't given enough information on "how long will this command
> take?"
>
> If it is less than a few seconds, then I would suggest just let your
> GUI freeze for that bit of time and use backticks to get your output.
For the upcoming demo version, I don't mind letting the GUI freeze. But
yes, the command does take more than 30secs to finish.
>
> sub runcmd {
> $mw->Busy(-recurse=>1);
> my $stuff = `$cmd`;
> $text->insert('end',$stuff);
> $text->see('end');
> $mw->Unbusy;
> }
> __END__
Thank you for the sample code. It worked perfectly in DOS, but not under
cygwin. Any thoughts on that?
S
|
|
|
|
|