Tyilo
Tyilo

Reputation: 30122

Redirect to both screen and pipe

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.

Upvotes: 5

Views: 1036

Answers (6)

Michał Šrajer
Michał Šrajer

Reputation: 31182

I like pv output. It's similar to how wget shows it's progress.

ubuntu@ubuntu:~$ dd if=/dev/urandom bs=1M count=100 | pv | cat >/dev/null
  14MB 0:00:03 [4,84MB/s] [  <=>                                              ]

If you know the size of data to be transfered you can specify it pv -s it can even show estimates:

ubuntu@ubuntu:~$ dd if=/dev/urandom bs=1M count=100 | pv -s 100M | cat >/dev/null
  14MB 0:00:03 [4,84MB/s] [===>                               ] 14% ETA 0:00:18

Upvotes: 1

Soren
Soren

Reputation: 14708

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)

Upvotes: 2

Tom Zych
Tom Zych

Reputation: 13586

If you want to copy to standard output and files, the tee command is your friend. If you want to pipe it to another command instead of a file, you could make the file /dev/tty (i.e., the screen), and pipe standard output to the other program.

Upvotes: 1

tripleee
tripleee

Reputation: 189729

If you just want a progress indicator, how about pv?

Upvotes: 1

krtek
krtek

Reputation: 26607

You can use tee.

Upvotes: 1

David Moreno Garc&#237;a
David Moreno Garc&#237;a

Reputation: 4523

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 '.'

Upvotes: 3

Related Questions