Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用命名管道创建';循环';_Linux_Bash_Shell_Pipe - Fatal编程技术网

Linux 使用命名管道创建';循环';

Linux 使用命名管道创建';循环';,linux,bash,shell,pipe,Linux,Bash,Shell,Pipe,我对shell脚本非常陌生,我正在努力掌握管道。我可能会朝着完全错误的方向走 我有一个shell脚本,它包含一个简单的while true循环,在这个循环中,我让netcat监听指定的端口,并将输入管道传输到一个二进制文件,该文件正在等待通过stdin发出命令。这是脚本A 我有第二个shell脚本,它接受输入作为参数,然后将这些参数回显到netcat正在侦听的端口。这是脚本B 我的目标是通过Netcat将位于Script-A中的二进制文件返回的输出获取到Script-B中,以便可以通过stdou

我对shell脚本非常陌生,我正在努力掌握管道。我可能会朝着完全错误的方向走

我有一个shell脚本,它包含一个简单的while true循环,在这个循环中,我让netcat监听指定的端口,并将输入管道传输到一个二进制文件,该文件正在等待通过stdin发出命令。这是脚本A

我有第二个shell脚本,它接受输入作为参数,然后将这些参数回显到netcat正在侦听的端口。这是脚本B

我的目标是通过Netcat将位于Script-A中的二进制文件返回的输出获取到Script-B中,以便可以通过stdout返回。二进制文件必须初始化并等待输入

这就是我所拥有的:

脚本A

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

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

foo=$(echo "$*" | nc localhost 1234)
echo "$foo"
通过此设置,二进制文件的输出通过脚本A完成 在做了一些研究之后,我达到了这一点,我试图使用一个命名管道创建一种从二进制文件返回到netcat的循环,它仍然不起作用-

脚本A

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

while true; do
    nc -kl 1234 < foobar | /binarylocation/ --readargumentsfromstdinflag > foobar
done
mkfifoobar
虽然真实;做
nc-kl 1234foobar
完成
脚本B没有改变


请记住,我的shell脚本编写经历是在大约一天的时间内完成的,谢谢。

问题出在脚本B中。。netcat从STDIN读取数据,并在STDIN关闭时立即退出,而不是等待响应

当您这样做时,您会意识到:

foo=$( ( echo -e "$*"; sleep 2 ) | nc localhost 1234) 
echo "$foo"
nc
有一个stdin行为参数

 -q    after EOF on stdin, wait the specified number of seconds and 
       then quit. If seconds is negative, wait forever.`
所以你应该:

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

问题出在脚本B中。。netcat从STDIN读取数据,并在STDIN关闭时立即退出,而不是等待响应

当您这样做时,您会意识到:

foo=$( ( echo -e "$*"; sleep 2 ) | nc localhost 1234) 
echo "$foo"
nc
有一个stdin行为参数

 -q    after EOF on stdin, wait the specified number of seconds and 
       then quit. If seconds is negative, wait forever.`
所以你应该:

foo=$( echo -e "$*" | nc -q5 localhost 1234) 
echo "$foo"
谢谢你的回答,我有几天不能回去测试了。我没有忘记这个问题——别担心!工作完美:-)谢谢你的耐心和坚持!谢谢你的回答,我有几天不能回去测试了。我没有忘记这个问题——别担心!工作完美:-)谢谢你的耐心和坚持!