Link to home
Start Free TrialLog in
Avatar of Metacrawler
MetacrawlerFlag for Germany

asked on

How to get the output of 7zip piped to another program

Hello Community,

I'm using Gentoo Linux and try to write a bash script that compresses a directory using 7zip. While compressing the script should show the progress using the 'dialog' tool.

First of all I count the number of files in that directory using 'find' and 'wc'. That's easy. After that I start 7zip to put the files in that directory into an archive. Until now, there is now problem. 7zip writes every file it processes to stdout and I can see it compressing file after file. The problem is that the shell seems to store the output when I try to pipe it to another process.

Simple Example:
7z a -bd archive.7z <dir-to-backup> | while read line ; do echo "${line}" ; done

When I execute this little script, I see nothing at first... but after 7zip has compressed some files all output is done at one go. After that there is no output for a certain time. 7zip works and consumes a lot of cpu time, but the output seems to be buffered in the pipe or somewhere else. After some time, I guess when the pipe is running full, 7zip's output becomes visible.

Neither zip nor tar does behave in that strange way. Their output can be seen line after line...

Does anyone know how I can solve that problem?
ASKER CERTIFIED SOLUTION
Avatar of Morne Lategan
Morne Lategan
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Correct, 7z seems buffering stdout if it's not a terminal (man isatty).
Avatar of Metacrawler

ASKER

Thank you very much. That was exactly what I was looking for. It works like a charm :-)