Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to both screen and pipe

Tags:

bash

I want to pipe some output to another program and display a progress bar.

The code would look something like this:

echo "Progress:"
(for i in {1..10}; do echo $i; echo "." > screen; sleep 1; done) | xargs echo

where screen would direct it to the screen. This is not working because it will just write the dots to the file screen.

What I want to do is outputting the "." while the script is running and piping all the echo "$i" at once at the end, so only one piping occurs.

like image 721
Tyilo Avatar asked Sep 10 '11 16:09

Tyilo


2 Answers

You have to send the echo to the tty device. For example, echo 'somthing' > /dev/tty

But if you only want to show dots in the screen you don't need any redirection. Only echo '.'

like image 187
David Moreno García Avatar answered Sep 19 '22 18:09

David Moreno García


Try to use the /dev/stderr for writing stuff to the screen

E.g. something like this should do it.

echo "Progress:"
(for i in {1..10}; do echo $i; echo -n "." | tee /dev/stderr ; sleep 1; done)
like image 36
Soren Avatar answered Sep 20 '22 18:09

Soren