使用命名管道来创建一个“环” [英] Using named pipes to create a 'loop'

查看:133
本文介绍了使用命名管道来创建一个“环”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的shell脚本,我试图让与管道交手。我可以完全错误的方向在这里标题中...

I'm very new to shell scripting and I am trying to get to grips with piping. I could be heading in completely the wrong direction here...

我已经是一个包含简单的shell脚本,而真正的循环,这个循环我得到的netcat监听指定端口和管道输入到通过等待标准输入的命令行二进制文件的范围内。这是脚本-A

What I have is a shell script that contains a simple while true loop, within this loop I am getting netcat to listen on a specified port and piping input to a binary file that is awaiting for commands through stdin. This is Script-A

我有接受输入作为参数第二shell脚本,然后将其回声这些参数到netcat的监听的端口。这是脚本-B

I have a second shell script that accepts input as arguments, it then echos those arguments to the port that netcat is listening on. This is Script-B

我的目的是让从位于脚本-A到通过Netcat的脚本-B二进制文件返回的输出,以便它可以通过标准输出返回。二进制文件必须被初始化并等待输入。

My aim is to get the returning output from the binary file located in Script-A into Script-B via Netcat so that it can be returned via stdout. The binary file has to be initialized and awaiting input.

这是我有:

脚本-A

while true; do
    nc -kl 1234 | /binarylocation/ --readargumentsfromstdinflag
done

脚本-B

foo=$(echo "$*" | nc localhost 1234)
echo "$foo"

通过这种设置,二进制文件的输出是通过脚本-A完成
做一些研究,我得到了这一点之后,我尝试使用命名管道,创建从二进制文件回netcat的一种循环,它仍然没有工作 -

With this setup, the output of the binary file is done via Script-A After doing some research I got to this point, I am trying to use a named pipe to create a sort of loop from the binary file back to netcat, it's still not working -

脚本-A

mkfifo foobar

while true; do
    nc -kl 1234 < foobar | /binarylocation/ --readargumentsfromstdinflag > foobar
done

脚本-B没有改变。

Script-B hasn't changed.

请记住我的shell脚本的经验茎一段关于一个单一的一天,谢谢。

Bear in mind my shell scripting experience stems over a period of about a single day, thank you.

推荐答案

问题是在你的脚本B .. netcat的从STDIN读取和STDIN时被关闭,而不是等待响应立即退出。

The problem is in your script B.. netcat reads from STDIN and exits immediately when STDIN is closed, not waiting for the response.

当你这样做,你会发现:

you will realize when you do this:

foo=$( ( echo -e "$*"; sleep 2 ) | nc localhost 1234) 
echo "$foo"

NC 有标准输入行为的参数。

nc has a parameter for the stdin behaviour..

 -q    after EOF on stdin, wait the specified number of seconds and 
       then quit. If seconds is negative, wait forever.`

所以,你应该做的:

So you should do:

foo=$( echo -e "$*" | nc -q5 localhost 1234) 
echo "$foo"

这篇关于使用命名管道来创建一个“环”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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