Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

manipulate file error

Status
Not open for further replies.

denis60

Technical User
Apr 19, 2001
89
CA
Hi!
I have a corrupted compressed file. It opened well but after reading 3 lines it failed and return:
zcat: /home/denis/jf/jftemp/240DU050725230311JF.Z: corrupt input.

How can i control the error message.

Here my script:
#!/usr/bin/perl
$var="/home/denis/jf/jftemp/240DU050725230311JF.Z";
open(IN,"zcat $var|") || die "error $!";
while(<IN>){
print $_;
}
close IN;
 
what is the "zcat" for? Is that a unix command? Try without that and see if you get what you want.
 
You could try to put an "eval" block around your code and catch errors that way. You can then check $@ outside the eval block for know error messages and replace them with your own.

Let us know how you get on.


Trojan.
 
crude, but if you're dying anyway:
Code:
my $tmp = "/tmp/zcat.$$.out";
open(IN,"zcat $var 2>$tmp |") or die "error $!: " . `cat $tmp`;

PS you almost always want "or die" rather than "|| die". The literal operator has much lower precedence than the symbolic one in is less likely to clobber the parsing of the rest of the line.

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
fishiface,
I don't think that opening the file is the problem.
I'm sure denis60 said that it opened fine and even allowed him to read the first 3 lines but THEN it failed.
That's why I suggested an eval around the while loop.



Trojan.
 
How can i control the error message.

I wasn't sure quite what was meant by "control", but I thought that getting it into a scalar was a good first step.

The eval won't pick up the error message as it is coming from the *nix utility zcat and is being written to fd2, which was being discarded. Actually capturing two fds simultaneously was addressed in an earier thread and is decidedly non-trivial without the intermediate tmp file technique. I don't like it but it certainly works and I'm not too bothered about performance or effciency in code that will only be run once, on the way out the door.

Having said that, its a confusing post because the real answer is probably "don't have a corrupt compressed file".

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Sorry i did a mistake

I tried something else:

#!/usr/bin/perl
$var="/home/denis/jf/jftemp/240DU050725230311JF.Z";
open(IN,"zcat $var|") or warn "error";
print "file opened\n";
print $var=<IN>;
close IN;
print "end\n";

and the answer is:
zcat: /home/denis/jf/jftemp/240DU050725230311JF.Z: corrupt input.
file opened
%%
end

I was wrong when i said the zcat error come from the third line(that what i saw on the screen).
The zcat "answer" come from the "open" line.
the open command work good, no warning are output.
In fact, the first line of the corrupt file is output(%%).
That why eval function don't find(i tried) anything.

Then my problem is to track the gzip message coming out from the open command.
I hope it's more clear.
 
If you don't want to use a tmp file as per my earlier suggestion and if you don't care about any non-error output from zcat, you could simply
Code:
open(IN,"zcat $var 2>&1 |") or die "error: can't open zcat: $!";
if ( my $error = <IN> ) { # zcat generated error output
  die $error;
}

Is there a reason why this wouldn't solve your problem?

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Have you tried binmode on the handle

Just a thought

cigless ...
 
Ok fishiface your script work fine thanks. I was trying many thing in same time and your solution was too close of my nose... i didn't see it sorry.

For the binmode way, i don't know how to use it with a compress file.

Thanks all for your help and supports.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top