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.

有帮助吗?

解决方案

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:

remote host -> nc -> regular pipe -> myprogram
myprogram -> mypipe -> nc -> remote host

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top