A common question newbies come up with is "how can I output text to another terminal or xterm?". Well the basic idea is pretty simple: open and xterm and type "tty" in it....it should return something like "/dev/pts/5". Then all you do to send data to it is (from another xterm) type " echo 'hello foobar' > /dev/pts/5".

The following snippet uses perl to automatically find the right /dev/pts/? The biggest obstacle is getting the right pid. When you open an xterm you can get it's pid from "fork()", but then it starts bash which is another pid. I have used time() to tag the xterm so it can be found and used as the parent pid of the bash instance. The ttydev is connected to the bash pid, not the xterm pid. Otherwise, it's straight forward.

#!/usr/bin/perl use warnings; use strict; use Proc::ProcessTable; my $time = time(); my $xtermparentpid; my $pttydev; system("xterm -T $time &"); #specify title so you can match for it my $t = new Proc::ProcessTable; foreach my $p (@{$t->table}) { # print "pid->",$p->pid," ttydev->",$p->ttydev," cmdline->", # $p->cmndline," ppid->",$p->ppid,"\n"; if($p->cmndline =~ /$time/){$xtermparentpid = $p->pid} } foreach my $p (@{$t->table}) { if($p->ppid == $xtermparentpid){$pttydev = $p->ttydev} } open(FH,">$pttydev") or warn $!; for(1..1000){print STDOUT $_,"\n"; print FH $_.'a',"\n";sleep 1;} close FH;

In reply to outputing data to another xterm by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.