如何将初始输入传递到流程中,然后该流程将是交互式的? [英] How can I pipe initial input into process which will then be interactive?

查看:100
本文介绍了如何将初始输入传递到流程中,然后该流程将是交互式的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够向交互过程的启动中添加初始命令,以便我可以执行以下操作:

I'd like to be able to inject an initial command into the launching of an interactive process, so that I can do something like this:

echo "initial command" | INSERT_MAGIC_HERE some_tool

tool> initial command 

[result of initial command] 

tool> [now I type an interactive command]

什么不起作用:

  • 仅传递初始命令中的命令无效,因为这将导致stdin没有连接到终端

  • Just piping the initial command in doesn't work, as this results in stdin not being connected to the terminal

写到/dev/pts/[number]会将输出发送到终端,而不是输入到进程,就像从终端一样输入

Writing to /dev/pts/[number] sends the output to the terminal, not input to the process as if it were from the terminal

除了缺点之外,还有什么呢?

What would but with disadvantages:

  • 执行一个命令,该命令派生一个孩子,写入其标准输入,然后转发其标准输入中的所有内容.缺点-终端控制功能(例如行模式与字符模式)将无法正常工作.也许我可以对伪终端的代理做些什么?

  • Make a command which forks a child, writes to its stdin and then forwards everything from its own stdin. Downside - terminal control things (like line vs character mode) won't work. Maybe I could do something with proxying of pseudo terminals?

使用命令行选项制作xterm的修改版(无论如何我都会为此任务启动一个),以便在遇到所需的提示字符串后注入其他命令.丑.

Make a modified version of xterm (I'm launching one for this task anyway) with a command line option to inject additional commands after encountering a desired prompt string. Ugly.

对我要运行的工具进行修改,以使其在命令行上接受初始命令.中断标准安装.

Make a modified version of the tool I'm trying to run so that it accepts an initial command on the command line. Breaks the standard installation.

(顺便说一下,当前感兴趣的工具是android的adb shell-我想在电话上打开一个交互式shell,自动运行命令,然后进行一个交互式会话)

(The tool of current interest, incidentally, is android's adb shell - I want to open an interactive shell on the phone, run a command automatically, and then have an interactive session)

推荐答案

您无需编写新工具来转发stdin-已经编写了一个工具(cat):

You don't need to write a new tool to forward stdin - one has already been written (cat):

(echo "initial command" && cat) | some_tool

这确实有将管道连接到some_tool而不是终端的缺点.

This does have the downside of connecting a pipe to some_tool, not a terminal.

这篇关于如何将初始输入传递到流程中,然后该流程将是交互式的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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