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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
09-10-2010, 12:54 AM
|
#1
|
LQ Newbie
Registered: Jun 2005
Posts: 22
Rep:
|
[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.
|
|
|
09-10-2010, 02:15 AM
|
#2
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
|
|
09-10-2010, 02:26 AM
|
#3
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Named pipes!
Make a pipe:
Then on any two terminals:
In terminal A.
This should just "hang"
Then in terminal B.
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.
|
09-10-2010, 02:32 AM
|
#4
|
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
|
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.
|
09-10-2010, 03:20 AM
|
#5
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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
|
|
|
09-10-2010, 03:36 AM
|
#6
|
LQ Newbie
Registered: Jun 2005
Posts: 22
Original Poster
Rep:
|
Thanks, worked nicely
|
|
|
09-10-2010, 04:37 AM
|
#7
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
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.
|
|
|
All times are GMT -5. The time now is 08:02 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|