The UNIX and Linux Forums  

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
Error with if statement..Please help jisha Shell Programming and Scripting 1 01-16-2008 07:13 AM
while read loop w/ a nested if statement - doesn't treat each entry individually littlefrog Shell Programming and Scripting 7 12-11-2007 09:49 PM
if statement in a while loop bobo UNIX for Dummies Questions & Answers 2 11-07-2006 12:38 PM
if statement in for loop of a string Sniper Pixie UNIX for Dummies Questions & Answers 7 03-02-2006 07:28 AM
tar error statement legato UNIX for Dummies Questions & Answers 3 03-29-2005 10:58 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-19-2007
lumdev lumdev is offline
Registered User
  
 

Join Date: Sep 2006
Location: Belgium
Posts: 6
For loop statement - catch error

I'm having a question about for loops. (bash)

I have the following for example:

for file in `ls *.txt`
do
read file ...
done

Now when there is a file present there is no problem, now when there is no file present I get the following output in my standard mail box : "No such file or directory" Script is executed via crontab.

Now I want to catch the above error so I don't get it in my mail any more, but I have no idea how to do this.

I can make an if statement first "if [ -f *.txt ] ...", but there must be a better solution.

Thx.
  #2 (permalink)  
Old 09-19-2007
ajcannon ajcannon is offline
Registered User
  
 

Join Date: Aug 2007
Location: Binfield, Berkshire. UK
Posts: 91
if

I think the use of the 'if' statement is a perfectly reasonable solution. You need some kind if conditional statement to determine whether or not your file exists and an 'if' would be OK
  #3 (permalink)  
Old 09-19-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by lumdev View Post
I can make an if statement first "if [ -f *.txt ] ...", but there must be a better solution.
Check for the actual file in the loop


Code:
  for file in *.txt
  do
     if test -f $file
     then
        read file ...
     fi
  done

  #4 (permalink)  
Old 09-19-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,378
Quote:
Originally Posted by lumdev View Post
I'm having a question about for loops. (bash)

I have the following for example:

for file in `ls *.txt`

That is not the way to loop through files. Not only is ls unnecessary, it will break your script if any filenames contain spaces or other pathological characters. Use the wildcard directly:


Code:
for file in *.txt

Quote:
do
read file ...
done

Now when there is a file present there is no problem, now when there is no file present I get the following output in my standard mail box : "No such file or directory" Script is executed via crontab.

Now I want to catch the above error so I don't get it in my mail any more, but I have no idea how to do this.

I can make an if statement first "if [ -f *.txt ] ...", but there must be a better solution.

No, you cannot do that; it will fail if there is more than one .txt file.

You can use a function:


Code:
is_file() {
   test -f "$1"
}

is_file *.txt &&
 for file in *.txt
 do
   ...
 done

The safest way is to check each file:


Code:
for file in *.txt
do
  [ -f "$file" ] || continue
  ...
done

  #5 (permalink)  
Old 09-20-2007
lumdev lumdev is offline
Registered User
  
 

Join Date: Sep 2006
Location: Belgium
Posts: 6
Thx for the answers.

I used the solution with the function, now I don't get any "No such file or directory" output anymore.

Thx for the help cfajohnson.
Closed Thread

Bookmarks

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 03:23 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