0

The "|" pipe operator connects the stdout of one process to the stdin of another. Is there any way to create a pipe that connects the stderr of one process to the stdin of another keeping the stdout alive in my terminal? Searching on the internet gave me no information at all...

Thank you in advance, Michalis.

4 Answers 4

3

You could use named pipes:

mkfifo /my/pipe
error-handler </my/pipe &
do-something 2>/my/pipe

This should keep stdin & stdout of "do-something" in your terminal und redirect stderr to /my/pipe, which is read by "error-handler".

(I hope this work, have no bash to test)

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

Comments

3

If you're happy to mix stdouot and stderr, then you can first redirect stderr to stdout and then pipe that:

theprogram 2>&1 | otherprogram

If you don't want stdout, you can kill that one:

theprogram 2>&1 1> /dev/null | otherprogram

If you do want to store the original stdout as well, then you have to redirect it either to a file (in place of /dev/null), or to another file descriptor that you opened previously with exec. Here are some details.

(Unfortunately there is no direct "pipe this file descriptor" syntax like 2|. That would have been handy.)

3 Comments

Thank you very much for your answer but in fact I wand to keep alive the stdout and pass only stderr to the other process. Is there any way to such a thing?
Do you mean you want to print stdout to the console? You can easily store it in a file... to print it back to the console, I'm not sure, you may have to set up a couple of new file descriptors and redirect appropriately... check that link I posted.
Yes, of course. Here are the actual commands: exec 3>&1; porc1 2>&1 >&3 3>&- | proc2 3>&-; exec 3>&-
3

You can get this effect with bash's process substitution feature:

somecommand 2> >(errorprocessor)

Comments

2

You may also swap the stdout & stderr streams, i. e. stdout becomes the new stderr and stderr becomes the new stdout).

# only the stdout stream gets upcased
ls -ld / xxx ~/.bashrc yyy 3>&1 1>&2 2>&3 3>&- | tr '[[:lower:]]' '[[:upper:]]'

# block original stdout by closing fd 1
ls -ld / xxx ~/.bashrc yyy 2>&1 1>&- | tr '[[:lower:]]' '[[:upper:]]'

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.