I know that running cat
with no argument reflects the user input
$ cat
test
test
reflected
reflected
I want to pipe the reflected output to another program such as base64
. The expected behavior is like so
$ cat | base64
test
dGVzdA==
another
YW5vdGhlcg==
I want this for encoding text line-by-line as I type, and/or send them over something like nc
. However, when used like this, no output seems to be reflected and escaping with ctrl+C
just terminates the whole thing without output
$ cat | base64
test
fail
^C
With everything working correctly, I should be able to establish an encoded/encrypted connection (very simple chat application) like so
# client
$ $CMD | encrypt | nc $SERVER $PORT
this is a test
multiple lines
^C
# host
$ nc -lvp | decrypt
this is a test
multiple lines
Similarly, I should be able to encode & save as follows
$ CMD | base64 | tee log_file
test
dGVzdA==
another
YW5vdGhlcg==
^C
$ cat log_file
dGVzdA==
YW5vdGhlcg==
Note that the whole thing should be a single pipe line. Using a loop wouldn't work well as nc
would establish a new connection every iteration and tee
without -a
would overwrite the file every line (per iteration). I want 1 single instance of the final command (e.g. nc
, base64
) taking input pipe from CMD
like with cat
but with user input instead of a file.
I'm looking for a way to do said piping of user input line-by-line to another process, preferably as a short one-liner. How can I get such piping of user input?
while read x; do echo "$x" | base64; done | nc ...
seems to be what you're looking for.while
loop), and (2) the target command (base64
,nc
, etc.) and make those 2 separate. This is so that anything that would take input fromstdin
can all be put on the same side for more complex implementations of such a pipe line.while
loop in a function which takes the first command as an argument, and then you pipe the output of the function to the second command.