LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-10-2010, 12:54 AM   #1
ahtoot
LQ Newbie
 
Registered: Jun 2005
Posts: 22

Rep: Reputation: 1
[Bash] Send command to another terminal


I open up 2 xterms on my desktop, A(/dev/pts/0) and B(/dev/pts/1).

I can write from A to B using redirection e.g. echo "test" > /dev/pts/1

How do I run a command from A on B? e.g. "clear"


Basically I'm putting the 2 terminals side by side, and using terminal B to display the contents of the current working directory, by running the following in A:

export PROMPT_COMMAND="ls -a > /dev/pts/1"

but this fills up the screen pretty fast. I was actually looking for a way to clear up the second terminal.

Any advice? Thanks.

Last edited by ahtoot; 09-10-2010 at 10:47 AM.
 
Old 09-10-2010, 02:15 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'm no expert, but I don't think there's any simple way to do that. > simply redirects the stdout of one process to the stdin of another. Since you're not placing anything into the readline of the terminal, it can't actually execute anything.

Perhaps it would be possible to write a simple script that would run on the receiving end, which would parse the incoming data before echoing it to the screen, and run the clear command whenever that input contains a specified string.
 
Old 09-10-2010, 02:26 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Named pipes!

Make a pipe:
Code:
mkfifo mypipe

Then on any two terminals:

In terminal A.
Code:
eval $(cat mypipe)
This should just "hang"

Then in terminal B.
Code:
echo "ls" > mypipe
At this point you should get a directory listing on terminal A.

You should be able to adapt this to your needs.

After you are done you can simply remove the named pipe with rm.

Cheers,

Evo2.
 
1 members found this post helpful.
Old 09-10-2010, 02:32 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi,

evo2 beat me to this one, but here's a more complete description to add to his post.

Kind regards,

Eric
 
1 members found this post helpful.
Old 09-10-2010, 03:20 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Using evo2's named pipe suggestion, I whipped up this quick script. Any line fed into the pipe that begins with an @ symbol will be read as a command and executed. @exit will remove the pipe and exit the script. Everything else will be echoed straight to stdout. I set the pipe name to /tmp/catpipe by default, but you can easily change it.

Code:
#!/bin/bash

PIPE=/tmp/catpipe
trap "rm -f $PIPE" exit 1
[[ ! -p $PIPE ]] && mkfifo $PIPE

while true; do
     while read line; do
          case "$line" in
               @exit) rm -f $PIPE && exit 0;;
               @*) eval "${line#@}" ;;
               * ) echo "$line" ;;
          esac
     done <$PIPE
done

exit 2
Edit: Whaddyaknow, pretty much the same solution as in EricTRA's link. In fact, I've gone ahead and incorporated the trap command in my script too, to make sure it cleans up when force-terminating the loop.

Last edited by David the H.; 09-10-2010 at 03:43 AM. Reason: as above + a bit of re-formatting
 
Old 09-10-2010, 03:36 AM   #6
ahtoot
LQ Newbie
 
Registered: Jun 2005
Posts: 22

Original Poster
Rep: Reputation: 1
Thanks, worked nicely
 
Old 09-10-2010, 04:37 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Glad to hear it.

When you've decided that the solution is satisfactory, please mark the thread as solved and pass around a few thank-yous.
 
  


Reply

Tags
bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I resize the terminal window from the command line in the gnome terminal QuIcKSpArK Linux - Newbie 5 04-21-2012 02:04 PM
[SOLVED] Make linux terminal transparent with terminal command? yooy Linux - Newbie 1 05-11-2010 03:24 AM
can't send mail from terminal packets Linux - Software 2 03-26-2007 03:14 AM
Is there any specific command for clear cache in bash terminal? green_njk Linux - Software 2 11-23-2006 10:22 PM
If bash command can send message to a host ? naihe2010 Programming 4 10-29-2005 03:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration