Bash 脚本语句
我试图弄清楚 bash 脚本文件中一行的含义:
mkfifo mypipe
nc -l 12345 < mypipe | /home/myprogram > mypipe
这是我的理解: nc -l 部分在端口 12345 上创建类似服务器端的行为,该行为从 mypipe 接收输入,将输出通过管道传输到程序,它将程序输出传输回 mypipe。
我的问题首先是我的分析正确吗?其次,mkfifo到底是什么,它是什么样的文件?我也不明白 nc -l 到底输出什么以便通过管道输入 myprogram。
感谢您的任何帮助。
I'm trying to figure out what a line means in a bash script file:
mkfifo mypipe
nc -l 12345 < mypipe | /home/myprogram > mypipe
Here's what I understand: nc -l part creates a server-side like behavior on port 12345, which takes in input from mypipe, which pipes that output to a program, which pipes the program output back into mypipe.
My question is firstly is my analysis correct? Second, what exactly is the mkfifo, like what kind of file is it? I also don't understand what nc -l outputs exactly in order to pipe into the myprogram.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mkfifo
创建一个管道文件。这里,FIFO的意思是“先进先出”。无论一个进程向管道中写入什么内容,第二个进程都可以读取。它不是一个“真正的”文件——数据永远不会保存到磁盘上;但是 Linux 将许多机制抽象为文件,以简化事情。nc -l 12345
将绑定到套接字 12345 并监听;当它捕获传入连接时,它将把标准输入传递到远程主机,并将远程主机的传入数据传递到标准输出。因此,这里的架构是:
有效地让 myprogram 和远程主机对话,即使 myprogram 被设计为从 stdin 读取并写入 stdout。
由于 bash 管道 (
|
) 仅处理一个方向的通信,因此您需要创建第二个显式管道来进行双向进程间连接。mkfifo
creates a pipe file. Here, FIFO means "first-in, first-out". Whatever one process writes into the pipe, the second process can read. It is not a "real" file - the data never gets saved to the disk; but Linux abstracts a lot of its mechanisms as files, to simplify things.nc -l 12345
will bind to socket 12345 and listen; when it catches an incoming connection, it will pass the standard input to the remote host, and the remote host's incoming data to the standard output.Thus, the architecture here is:
effectively letting myprogram and remote host talk, even though myprogram was designed to read from stdin and write to stdout.
Since the bash pipe (
|
) only handles one direction of communication, you need to make an explicit second pipe to do bidirectional inter-process connection.