开发者

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, s开发者_如何学JAVAo only one piping occurs.


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


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)


You can use tee.


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


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜