Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux - write commands from one terminal to another

I need to write commands from one terminal to another terminal.

I tried these:

echo -e "ls\n" > /proc/pid/fd/0
echo -e "ls\n" > /dev/pts/4

Which just prints the ls as output and doesn't execute.

I tried these:

chmod 777 /dev/tty4 ;echo "ls" > /dev/tty4
chmod 777 /dev/tty40 ;echo "ls" > /dev/tty40

Which don't seem to do anything

Any ideas?

[note that I don't want to touch the second terminal to accomplish this. only the first one]

like image 222
user1364700 Avatar asked Apr 29 '12 21:04

user1364700


People also ask

How do you write a command in Linux terminal?

Linux operating system allows users to create commands and execute them over the command line. To create a command in Linux, the first step is to create a bash script for the command. The second step is to make the command executable. Here, bashrc means run the Bash file.

How do I connect two terminals in Linux?

What you can do: create a new tab in the window that you want to merge into the first one, then choose the first tab and drag it to that window and close the other window.


2 Answers

Python code:

#!/usr/bin/python

import sys,os,fcntl,termios
if len(sys.argv) != 3:
   sys.stderr.write("usage: ttyexec.py tty command\n")
   sys.exit(1)
fd = os.open("/dev/" + sys.argv[1], os.O_RDWR)
cmd=sys.argv[2]
for i in range(len(cmd)):
   fcntl.ioctl(fd, termios.TIOCSTI, cmd[i])
fcntl.ioctl(fd, termios.TIOCSTI, '\n')
os.close(fd)
like image 167
esoriano Avatar answered Sep 28 '22 15:09

esoriano


Is posible to show the output of a command on multiple terminals simultaneously with the following script., and it works with all console programs, including the editors. For example doing:

execmon.bash  'nano hello.txt' 5

Open an editor and both the output and the text that we introduce will be redirected to the virtual terminal number 5. You can see your terminals:

ls /dev/pts

Each virtual terminal has an associated number.

Is work with the normal terminal, konsole and xterm, just create the file execmon.bash and put this:

#! / bin / bash
# execmon.bash
# Script to run a command in a terminal and display the output
# in the terminal used and an additional one.
param = $ #
if [$ param-eq 2]; Then
    echo $ 1 | tee a.out a.out && cat> / dev / pts / $ 2 && exec `cat` a.out | tee / dev / pts / $ 2 && rm a.out
else
    echo "Usage:"
    echo "execmon 'command' num '
    echo "-command is the command to run (you have to enter ')"
    echo "-num is the number of virtual console to output to the"
fi

Example:

execmon.bash 'ls-l' 5
execmon.bash 'nano Hello.txt' 5
like image 22
Voislav Sauca Avatar answered Sep 28 '22 17:09

Voislav Sauca