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
Loop through found files Calypso Shell Programming and Scripting 11 04-22-2009 04:03 PM
for loop not working - syntax error at line 6: `end of file' unexpected debojyoty Shell Programming and Scripting 3 04-13-2009 09:31 AM
Error 15-the file was not found-update linux10.0 failure nandhusky SuSE 0 01-16-2009 10:36 AM
find command in while loop - how to get control when no files found? mavsman Shell Programming and Scripting 3 04-03-2008 11:44 AM
variable not found with loop? douknownam Shell Programming and Scripting 2 09-27-2004 10:59 AM

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 05-09-2009
earls earls is offline
Registered User
  
 

Join Date: May 2009
Posts: 1
file not found error during loop

Hi,
I have a strange problem and I'm sure its because I'm doing something stupid.
When I loop through all of the files in a directory they cannot be found.

Any help would be greatly appreciated.

Thanks,

-E


$ touch a.txt
$ touch b.txt
$ ls
a.txt b.txt
$ for f in `ls` ; do echo file $f; done
file a.txt
file b.txt
file
$ for f in `ls` ; do file $f; done
a.txt: ERROR: cannot open `a.txt' (No such file or directory)
b.txt: ERROR: cannot open `b.txt' (No such file or directory)
: ERROR: cannot open `' (No such file or directory)
$ file a.txt
a.txt: empty
$
  #2 (permalink)  
Old 05-09-2009
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 427
please post the output of ls -lib
  #3 (permalink)  
Old 05-09-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Try giving quotes

Code:
 for f in `ls`; do file "$f"; done
  #4 (permalink)  
Old 05-09-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by earls View Post
Hi,
I have a strange problem and I'm sure its because I'm doing something stupid.
When I loop through all of the files in a directory they cannot be found.

Any help would be greatly appreciated.

When you post code, please wrap it in [code] tags.

And please do not change the size of the type; you made it too small to be readable. Not to mention adding dozens of tags that had to be removed before I could edit the post.
Quote:
Code:
 
$ touch a.txt
$ touch b.txt
$ ls
a.txt b.txt
$ for f in `ls` ; do echo file $f; done

The is not the way to loop through a list of files. Not only is ls unnecessary, but it will break the script if any filenames contain spaces or special characters.
Code:
for f in *;  do echo file "$f"; done
Quote:
Code:
file a.txt
file b.txt
file
$ for f in `ls` ; do file $f; done

Code:
for f in * ; do file "$f"; done
Quote:
Code:
a.txt: ERROR: cannot open `a.txt' (No such file or directory)
b.txt: ERROR: cannot open `b.txt' (No such file or directory)
: ERROR: cannot open `' (No such file or directory)
$ file a.txt
a.txt: empty
$

Note, however, that you don't need the loop in either case

Code:
printf "%s\n" *

file *
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 09:06 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