Output of tar to a filehandle

W

Whitey Johnson

I have a small backup script that tars up a dir and puts it on a different
drive. I want to get the output of tar (the list of files being tarred)
into a file. I can do it by assigning STDOUT to a file like so:

open STDOUT, ">> $logfile" or die "Can't open the log file: $!\n";
system "tar", "cvzf", $bkfile, $dir;

Question: How do I return STDOUT back to the screen after the backup has
run so I can print a message to the screen?

I have tried selecting a file like this:
open LOGFILE, ">> $logfile" or die "Can't open the log file: $!\n";
select LOGFILE;
system "tar" yada yada..
but the output goes to the screen not the file.
I have also tried a whole different method like this:
open LOGFILE, ">> $logfile" or die "Can't open the log file: $!\n";
open TAR, "tar -cvzf $bkfile $dir" or die "Tar failed: $!\n";
print LOGFILE while (<TAR>);
but the I get an error that the directory doesn't exist.

TIA
 
T

Tad McClellan

Whitey Johnson said:
open STDOUT, ">> $logfile" or die "Can't open the log file: $!\n";
system "tar", "cvzf", $bkfile, $dir;

Question: How do I return STDOUT back to the screen after the backup has
run so I can print a message to the screen?


If you don't change STDOUT, then your question becomes moot.

So don't change STDOUT. :)

system "tar cvzf $bkfile $dir >>$logfile";
 
W

Whitey Johnson

If you don't change STDOUT, then your question becomes moot.

So don't change STDOUT. :)

system "tar cvzf $bkfile $dir >>$logfile";

Thanks!
I read in the the llama book that putting all the scalars in quotes
with a system function could get a bit scary if the user was feeding the
progam input but I guess since I am defining the variables it doesn't
matter.

Question 2: Would I be able to use the:
open TAR, "tar -cvzf $bkfile $dir" or die "Tar failed: $!\n";
with scalars? I can do it if I state the file and directory in the command
like:
open TAR, "tar -cvzf file.tar.gz /home/john" or die ...
but I have the $bkfile name created with the date it is run.
 
A

A. Sinan Unur

Hmmmm ...
....

Question 2: Would I be able to use the:
open TAR, "tar -cvzf $bkfile $dir" or die "Tar failed: $!\n";

Maybe you should reexamine the coherency of your own mutterings. This is
gibberish. Don't write gibberish. Instead, read, I mean actually read,
Tad's response.

Sinan.
 
W

Whitey Johnson

On Thu, 04 Nov 2004 20:48:44 +0000, A. Sinan Unur muttered incoherently:

Just a joke I don't actually think Tad (or you now) muttered anything
incoherently.

...


Maybe you should reexamine the coherency of your own mutterings. This is
gibberish. Don't write gibberish. Instead, read, I mean actually read,
Tad's response.

Sinan.

Ouch.

Well I figured it out. I was missing some ()'s and a |.
open (TAR, "tar -cvzf $bkfile $dir |") or die "Tar failed: $!\n";

It was something I had found while googling this group yesterday and
wanted to figure it out for the whole "There's more than one way to do it"
thing. Seems I copied it wrong.
 
T

Tad McClellan

Whitey Johnson said:
Question 2: Would I be able to use the:
open TAR, "tar -cvzf $bkfile $dir" or die "Tar failed: $!\n";
with scalars?


What happened when you tried it?

You _do_ read the documentation for the functions that you use, don't you?


perldoc -f open
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top