注射按键来使用bash不同工艺 [英] Inject Keystroke to different process using Bash

查看:111
本文介绍了注射按键来使用bash不同工艺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行下去,直到关键是pressed的过程。我想使用bash注入一个按键到这个过程有它终止。根据这个帖子,<一个href=\"http://stackoverflow.com/questions/10376251/linux-write-commands-from-one-terminal-to-another\">linux - 从一个终端到另一个我试图写命令使用

I have a process that runs indefinitely until a key is pressed. I would like to use bash to inject a keystroke into this process to have it terminate. Based on this post, linux - write commands from one terminal to another I have tried to use

echo -e "b" > /proc/[pid]/fd/0

(在这种情况下,字母b为只是任意的)的字母b将在被无限期地运行的进程的终端上显示,但像它,如果它不触发的程序的终止我其实b型到窗口。

(The letter "b" in this case is just arbitrary) The letter "b" will show up in the terminal of the process that is running indefinitely, but it doesn't trigger the termination of the program like it does if I actually type "b" into the window.

我也看到了xdotools的建议,但我无法得到它的工作,我试图从依靠GUI执行这一望而却步。

I have also seen the recommendation for xdotools, but I couldn't get it to work and am trying to stay away from relying on GUI for implementing this.

我运行Ubuntu 10.04,和我没有在bash太多经验。

I am running Ubuntu 10.04, and I don't have much experience in bash.

推荐答案

从<一个href=\"http://www.linuxquestions.org/questions/linux-software-2/how-do-i-stuff-a-keystroke-into-another-virtual-terminals-stdin-839110/\"相对=nofollow>这里:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main(void)
    {
    int hTTY = open("/dev/tty1", O_WRONLY|O_NONBLOCK);
    ioctl(hTTY, TIOCSTI, "b");

    close(hTTY);
    return 0;
    }

终端和击键是在本实施例硬$ C $光盘,但它可以适应的需要。

The terminal and keystroke are hardcoded in this example, but it can be adapted to your needs.

您可以做同样的事情在Perl:

You can do something similar in Perl:

perl -e '$TIOCSTI = 0x5412; $tty = "/dev/pts/1"; $char = "b"; open($fh, ">", $tty); ioctl($fh, $TIOCSTI, $char)'

我必须用须藤运行这些。

这篇关于注射按键来使用bash不同工艺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆