Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

shell - Ask a running bash (interactive) to run a command from outside

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use a TIOCSTI ioctl. Example in C:

char* cmd="ls
";
int fd = open (ptsname, O_RDWR);

while (*cmd)
{
    ioctl(fd, TIOCSTI, cmd++);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...