Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.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 bash使用符号运行nc并终止程序_Linux_Bash_Shell_Pipeline - Fatal编程技术网

Linux bash使用符号运行nc并终止程序

Linux bash使用符号运行nc并终止程序,linux,bash,shell,pipeline,Linux,Bash,Shell,Pipeline,我想通过&运行nc,然后随时从/proc文件系统手动将数据输入stdin。所以问题是: 如果我运行nc127.0.0.11234& 程序在后台运行,我可以用标准格式写任何我想写的东西。但是,如果我创建test.sh并添加 #!/bin/bash nc 127.0.0.1 1234 & sleep 20 它连接到1234并立即终止(甚至不等待20秒)。为什么?我怀疑它是从某个地方写的stdin。如果我正确理解了您的目的,您希望手动将数据提供给nc,然后将数据发送给客户端 为此,可以使用命

我想通过&运行nc,然后随时从/proc文件系统手动将数据输入stdin。所以问题是:

如果我运行
nc127.0.0.11234&

程序在后台运行,我可以用标准格式写任何我想写的东西。但是,如果我创建test.sh并添加

#!/bin/bash
nc 127.0.0.1 1234 &
sleep 20

它连接到1234并立即终止(甚至不等待20秒)。为什么?我怀疑它是从某个地方写的stdin。

如果我正确理解了您的目的,您希望手动将数据提供给nc,然后将数据发送给客户端

为此,可以使用命名管道

cat/tmp/f|/parser.sh2>&1|nc-lvk 127.0.0.11234>/tmp/f

其中
/tmp/f
是使用
mkfifo/tmp/f

无论您想要馈送到
nc
的是什么,都可以在
parser.sh

有趣的问题中回显

bash手册页声明:

   If  a  command  is  followed  by a & and job control is not active, the
   default standard input for the command is  the  empty  file  /dev/null.
   Otherwise,  the  invoked  command  inherits the file descriptors of the
   calling shell as modified by redirections.
如果在shell脚本(带有作业控制)之外调用
nc127.0.0.11234
,则会产生相同的结果

您可以这样更改bash脚本以使其正常工作:

#!/bin/bash
nc 127.0.0.1 1234 < /dev/stdin &
sleep 20
#/bin/bash
nc 127.0.0.1 1234&
睡20

感谢您提供了另一个解决方案,但除此之外,我想了解为什么nc会在我的casetry标志
-lk
中终止,该标志允许它不会在收到一个连接后立即终止并等待另一个连接@JaniBaramidze