For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2004 > Preventing shell redirection from creating empty file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Preventing shell redirection from creating empty file
Dan Fish

2004-10-27, 8:55 am

I appologize that this is more of a shell question (tcsh) than a perl
question, but I've found that most of the really good perl guru's are fairly
decent shell wizards as well :-)

I have a c-routine that calls a perl script something like this:

system("myscript.pl arg1 arg2 > myfile.txt");

What is the easiest way to prevent "myfile.txt" from being created if the
script dies or produces no output? (It seems the shell will always create
myfile.txt, regardless)

I can think of a couple of ways offhand...

1. Within the c-routine, check for an empty file after the script is run
and remove if empty.
2. Handle the output directly from within the perl script with
open/print/close (I.E. avoid redirection)

I was hoping some shell guru out there knows a more generic approach.

Thanks,
-Dan


---
Dan Fish - seadancr@cox.net
"A -good- dive buddy will be there if you run out of air, a -great- one will
be there before you run out!"

Chris Devers

2004-10-28, 3:57 pm

On Tue, 26 Oct 2004, Dan Fish wrote:

> system("myscript.pl arg1 arg2 > myfile.txt");
>
> What is the easiest way to prevent "myfile.txt" from being created if
> the script dies or produces no output? (It seems the shell will always
> create myfile.txt, regardless)


Would it be enough to just delete the file if it's empty?

du myfile.txt | grep '^0' && rm myfile.txt

That seems easier than any other approach i can think of.



--
Chris Devers
Jim

2004-10-29, 8:55 am


>
> On Tue, 26 Oct 2004, Dan Fish wrote:
>
> created if
> will always
>
> Would it be enough to just delete the file if it's empty?
>
> du myfile.txt | grep '^0' && rm myfile.txt
>
> That seems easier than any other approach i can think of.
>


How about something like this, in your perl program:
-z $file && unlink $file;



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.772 / Virus Database: 519 - Release Date: 10/1/2004


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com