I have a terminal (xterm) open with bash running in it, showing a prompt. Suppose I know this running bash's pid and the tty associated with this terminal. Is there any way, not touching this terminal at all, but only use the tty and pid information, to ask this very running bash to run a command? Naively echo "command" > tty would only show the command in the terminal but bash doesn't receive it as a user input.
Asked
Active
Viewed 1,750 times
3 Answers
5
Use a TIOCSTI
ioctl. Example in C:
char* cmd="ls\n";
int fd = open (ptsname, O_RDWR);
while (*cmd)
{
ioctl(fd, TIOCSTI, cmd++);
}

n. m. could be an AI
- 112,515
- 14
- 128
- 243
1
(2017) The following is practical from a script or command line:
stty -echo; perl -le 'require "sys/ioctl.ph"; ioctl(STDIN, &TIOCSTI, $_) for split "", join " ", @ARGV ' `_cmds_` ;stty echo; _app_
If you drop the final app the output of cmds will be present on the input queue (as it is called in the kernel), on your current console shell or in the application that shell
ed the command line. The stty
just stops any (full-duplex) echo to the screen.
Note: technically, this is not explicitly "simulating keypress".

Paul Wratt
- 51
- 5
0
but its command executing in the same terminal only not in the other terminal given by pts/no

Venkat
- 1
- 2
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 18 '22 at 12:29