2

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.

3
  • I think the < mypipe is largely irrelevant; it does mean there is a program with the FIFO open for reading, so that the open for /home/myprogram returns, but since nc is reading on the socket and not from its standard input, the FIFO will eventually fill (maybe after as little as about 4 KiB of data), blocking /home/myprogram, and thereafter blocking nc too (another 4 KiB or so, which is the capacity of the pipe), which then blocks whatever process is writing to port 12345. Commented Dec 20, 2011 at 6:34
  • @JonathanLeffler: It's hardly irrelevant. nc reads from both the socket and stdin, and passes the socket input to stdout and stdin input to the socket. You can test it yourself - echo "GET /" | nc www.google.com 80 will pass GET / to Google's server, and pass its response to your screen. Commented Dec 21, 2011 at 0:54
  • OK - I'm not sure I understand what nc does, then. It is nominally netcat, but cat doesn't read its standard input if it is given a file to read instead (and I assumed that -l 12345 meant listen on port 12345 and cat anything that comes in). If nc is different, so be it; I haven't used it more than once or twice - and that extremely casually. Treat my previous comments with an appropriate pinch of salt (possibly quite large). It is an unusual arrangement of I/O, regardless of what nc does; it would be interesting to know what /home/myprogram does with its standard input. Commented Dec 21, 2011 at 1:17

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.