Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: I want to call a special xterm to execute "ls" command in a perl script

by Anonymous Monk
on Aug 22, 2013 at 06:52 UTC ( #1050470=note: print w/ replies, xml ) Need Help??


in reply to Re^2: I want to call a special xterm to execute "ls" command in a perl script
in thread I want to call a special xterm to execute "ls" command in a perl script

Perl cannot normally control other applications' terminals, so you'll have your application to fork, then run the child process under xterm, then use Expect module to pass the "vst" command to the "TBTerm" program. Read perlop about quoting operators if you don't know how to quote your command properly.

Use something like this to run your application under xterm:

unless (-t STDIN) { # check whether STDIN is a terminal exec (qw/xterm -e/,$0); # assuming that your perl file is executab +le }
Do you really need to spawn another xterm? If not, you can simplify your application and avoid coping with returning text data from forked child process (read pipe, by the way), since Expect creates a virtual terminal (not visible as a graphical window, though) to run your application into.


Comment on Re^3: I want to call a special xterm to execute "ls" command in a perl script
Download Code
Reaped: Re^4: I want to call a special xterm to execute "ls" command in a perl script
by NodeReaper (Curate) on Aug 22, 2013 at 07:48 UTC
Reaped: Re^4 I want to call a special xterm to execute "ls" command in a perl script
by NodeReaper (Curate) on Aug 22, 2013 at 08:00 UTC
Re^4: I want to call a special xterm to execute "ls" command in a perl script
by sincerchan (Initiate) on Aug 22, 2013 at 08:43 UTC

    yes,I really want to spawn a xterm, because the TBterm cmd will pop up a xterm window with special setting.

    I think there are two things hard to realize:

    the first: how to open a process of "TBterm" backgroundly, because if it pop up window explicitly, it can't run the "vst" automaticly by script.

    the sencond: how to run the "vst" cmd in the background process "TBterm".(you can recognize "vst" as common cmd,like "ls")

    I have try follow module: use Proc::Daemon, Proc::Background; Expect, but all failed. now I don't know if my demand can be realized by pipe.

      I couldn't find any information on that "TBterm" application, so I'll have to ask more questions.

      I really want to spawn a xterm, because the TBterm cmd will pop up a xterm window with special setting.
      What kind of special settings are specified in xterm spawned by TBterm? Does it spawn an xterm when run in another terminal?
      how to open a process of "TBterm" backgroundly
      So, you need to run TBterm in background (with no visible terminal), or not in background, but in an xterm?

      I'll try to solve the problem as I understand it now, using sh as an interactive application and "ls" as a command:

      #!/usr/bin/perl use warnings; use strict; use Expect; print("STDIN is not a terminal, running xterm\n"), exec ('xterm', '-e' +, $^X, $0, @ARGV) unless -t STDIN; print "Surely running in a terminal\n"; my $expect = Expect::->spawn("/bin/sh") or die "bash: $!\n"; $expect->expect(1,"\$ "); # expect a command line prompt $expect->send("ls\n"); $expect->expect(1,"ls\r\n"); # sh echoes the entered command, so I hav +e to expect() it back # need to play with $expect->slave->stty() to get rid of \r $expect->expect(1,"\$ "); # expect another command line prompt meaning + that the command succeeded my $result = $expect->before(); # catch everything in between the echo +ed "ls" and new prompt $expect->send("exit\n"); # terminate the shell properly $expect->soft_close; print "Gathered output: BEGIN\n$result\nEND\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1050470]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-10-01 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (389 votes), past polls