mkfifo /tmp/f ; cat /tmp/f | /bin/bash -i 2>&1 | nc -l -p 1234 > /tmp/f
I am new to bash, I am trying to understand this piece of "code".
cat filePipe
by itself ONLY PRINTS ONE LINE, and then exits (I just tested it), and to make cat not to exit I do: while cat pipeFile ; do : ; done
. So how does that above work?When bash
parses the line mkfifo /tmp/f ; cat /tmp/f | /bin/bash -i 2>&1 | nc -l -p 1234 > /tmp/f
, several things happen. First, the fifo is created. Then, in no particular order, 3 things happen: cat
is started, bash
is started and nc
is started with its output stream connected to /tmp/f
. cat
is now going to block until some other process opens /tmp/f
for writing; the nc
is about to do that (or already did it, but we don't know if cat
will start before nc
or if nc
starts before cat
, nor do we know in which order they will open the fifo, but whoever does it first will block until the other completes the operation). Once all 3 processes start, they will just sit there waiting for some data. Eventually, some external process connects to port 1234 and sends some data into nc
, which writes into /tmp/f
. cat
(eventually) reads that data and sends it downstream to bash
, which processes the input and (probably) writes some data into nc
, which sends it back across the socket connection.
If you have a test case in which cat /tmp/f
only writes one line of data, that is simply because whatever process you used to write into /tmp/f
only wrote a single line. Try: printf 'foo\nbar\nbaz\n' > /tmp/f & cat /tmp/f
or while sleep 1; do date; done > /tmp/f & cat /tmp/f