The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
testing if files exist skooly5 UNIX for Dummies Questions & Answers 7 05-01-2008 10:10 AM
Check if certain files exist in a directory, if not add name to a textfile SunnyK Shell Programming and Scripting 1 02-07-2008 09:21 AM
How to check a file exist and do a copy of other files ahjiefreak Shell Programming and Scripting 6 12-20-2007 12:07 AM
Tab Completion showing files that Dont Exist dittonamed UNIX for Advanced & Expert Users 3 05-27-2003 12:50 PM
Files listed Do not really exist Ricky Raynor UNIX for Advanced & Expert Users 6 02-08-2002 05:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-20-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Question perl: When dealing with files that do not exist

I have a process run weekly where I must convert data formats for about thirty files. I read a text file that provides all of the filenames and switch settings.

My perl code is:

Code:
for ($j = 1; $j <= $k; $j++)
{

   open(FIN2,$fin2) || die "open: $!";

   do other stuff
}
Every once in a while, one of the files is empty, & this is ok.
But the perl program aborts at that open line. Right now, when this happens, I simply remove the file info from the text file listing, and restart. But, I must remember to put the line(s) back in for the next week.

QUESTION:
is it better to do a "next" when the file open fails, or
do some kind of File::stat command to know if the file exists first

In shell scripting, one can do an "if [ -e filename ] " kind of thing
  #2 (permalink)  
Old 02-20-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
It all depends on your requirements, but a 'next' will work fine:

Code:
for ($j = 1; $j <= $k; $j++)
{

   open(FIN2,$fin2) or do {warn "Can't open $fin2: $!"; next;};

   do other stuff
}
remove the do {warn} stuff if you don't need any error handling.
  #3 (permalink)  
Old 02-20-2008
HPAVC's Avatar
HPAVC HPAVC is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 106
Quote:
Originally Posted by joeyg View Post
In shell scripting, one can do an "if [ -e filename ] " kind of thing
you can if (-f "/etc/passwd") { warn "file exists" } in perl too, most of the modern test flags exist.

the big problem i have is when the file is after the mundane open(FH,..) that your doing. and the print FH "" fails. This take a little bit of care and feeding but its worth it imo.
Closed Thread

Bookmarks

« wc -l | Pid »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0