如何将bash输出写入任意程序stdin [英] How to write bash output to arbitrary program stdin

查看:172
本文介绍了如何将bash输出写入任意程序stdin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望(希望很容易)从bash脚本写入任何已经通过该程序的stdin运行的任意程序。

I want to (hopefully easily) write from a bash script to any arbitrary program that is already running via that program's stdin.

说我有一些任意程序总和,不断从终端获取用户输入。它从stdin接收的每个整数都会添加到当前总和并输出新的总和。这是我的意思的终止文本示例:

Say I have some arbitrary program "Sum", that constantly takes user input from the terminal. Every integer it receives from stdin it adds to the current sum and outputs the new sum. Here's example terminal text of what I mean:

$: ./Sum
Sum: 0
Give me an integer: 2
Sum: 2
Give me an integer: 5
Sum: 7

如何在bash脚本中自动执行此过程?如果我控制了Sum的源代码,我可以让它接受整数参数。但是如果我已经说过对程序的控制,我怎样才能自动与Sum交互?这是我想要要执行的bash片段的伪代码:

How would automate this process in a bash script? If I had control of Sum's source code I could let it accept integer arguments. But if I don't have said control of the program, how can I automate the interaction with Sum? Here's a psuedo code of a bash snippet of what I want to do:

在example.sh中:

In example.sh:

#!/bin/bash
my_program_input_point=./Sum &
echo 2 > my_program_input_point
echo 5 > my_program_input_point

因此,在我的终端屏幕上,它仍然如下所示:

Thus on my terminal screen it would still look like this:

$: ./example.sh
Sum: 0
Give me an integer: 2
Sum: 2
Give me an integer: 5
Sum: 7

区别在于我不会已经键入任何一个。

The difference is I wouldn't have typed any of it.

感觉这个任务应该非常简单,因为基本上你可以在终端中做任何事情,你也可以在脚本中轻松完成。显然,一旦开始,就会直接与任意流程进行交互。

It feels like this task should be really easy, because basically anything you can do in a terminal, you can also easily do in a script. Except, apparently, directly interact with an arbitrary process once its started.

本主题通过使用管道来回答我的问题的某些方面。但是,只有当我控制管道两侧的两个程序时,接受的答案才有效,收件人程序没有义务自动读取管道。

This topic answers some aspects of my question by using pipes. However the accepted answer only works if I have control of both programs on each side of the pipe, the "recipient" program is under no obligation to read from the pipe automatically.

这是假设我无法修改有问题的程序/脚本。我也不能写一个管道阅读器脚本来包装程序(Sum),因为那个包装器脚本仍然需要与正在运行的进程Sum进行交互。

This is assuming I can't modify the program/script in question. I also can't write a "pipe reader" script to wrap around the program (Sum), because that wrapper script would still be required to interact with the running process Sum.

在serverfault.com上这个问题的第二个答案(由jfgagne撰写) )似乎更接近,但我似乎无法让它工作。有没有更容易的方式来做我还不知道的事情?

The second answer to this question on serverfault.com (written by jfgagne) seems much closer, but I can't seem to get it working. Is there any easier way to do this that I just don't know about?

有关如何捕获和读取任意程序输出的信息,请参阅下一页问题了解更多信息

For information on how to capture and read an arbitrary program's output, see my next question for more information

推荐答案

您可以使用命名管道。无论什么开始 Sum 负责在某个地方方便创建命名管道,然后以命名管道为标准启动 Sum 输入。

You can use a named pipe. Whatever starts Sum is responsible for creating the named pipe some place convenient, then starting Sum with the named pipe as its standard input.

mkfifo /tmp/pipe
Sum < /tmp/pipe

然后你的脚本可以将管道的名称作为参数,然后对待它作为一个可以写入的文件。

Then your script could take the name of the pipe as an argument, then treat it as a file it can write to.

#!/bin/bash
p=$1

echo 2 > "$p"
echo 5 > "$p"

然后你可以用 client / tmp /调用你的脚本pipe

这篇关于如何将bash输出写入任意程序stdin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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