Redirect STDU output via command

All I want to do is just redirect the stdout executable to the channel. An example will explain this better than me.

$ echo "Hello world" | cowsay 

displays "Hello world" in cowsay, I want to pre-process the terminal / bash stdout to go through cowsay

 $ echo "Hello world" 

this should be displayed in the same way as the first command.

Thanks in advance.

+5
source share
2 answers

You can use process substitution:

 #!/bin/bash exec > >(cowsay) echo "Hello world" 

However, there are reservations. If you start something in the background, the cow will wait for completion:

 #!/bin/bash exec > >(cowsay) echo "Hello world" sleep 30 & # Runs in the background exit # Script exits immediately but no cow appears 

In this case, the script will exit without output. After 30 seconds, when sleep ends, the cow suddenly appears.

You can fix this by telling programs to write somewhere else so that the cow does not have to wait for them:

 #!/bin/bash exec > >(cowsay) echo "Hello world" sleep 30 > /dev/null & # Runs in the background and doesn't keep the cow waiting exit # Script exits and the cow appears immediately. 

If you are not running anything in the background, make one of your tools or programs. To find which ones, redirect or comment on them one by one until a cow appears.

+3
source

You can use a named pipe:

 mkfifo /tmp/cowsay_pipe cowsay < /tmp/cowsay_pipe & exec > /tmp/cowsay_pipe # Redirect all future output to the pipe echo "Hello world" 
+2
source

Source: https://habr.com/ru/post/1200613/


All Articles