file not found error during loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting file not found error during loop
# 1  
Old 05-09-2009
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  
Old 05-09-2009
please post the output of ls -lib
# 3  
Old 05-09-2009
Try giving quotes

Code:
 for f in `ls`; do file "$f"; done

# 4  
Old 05-09-2009
Quote:
Originally Posted by earls
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 *

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Fatal error: 'Xm/Xm.h' file not found , motif installed

Hello, i have installed open-motif -2.3.8_1 X11 Toolkit on freebsd32bit machine. wanna compile a little script. script is ok. already compiled on other machine (linux) the message is as follows cc -o button button.c -lXm -lXt -lX11 button.c:3:10: fatal error: 'Xm/Xm.h' file not found... (5 Replies)
Discussion started by: Sennenmut
5 Replies

2. Shell Programming and Scripting

Bash - trap error file not found

Hello. In bash, is there a way to trap error "file not found" when a script call another script which is not found; then abort. Example ( part of script running with -x option set) : + return 0 + RETURN_CODE=0 + ] + /root/bin/200_yast_install/00_reset_yast_install bash:... (5 Replies)
Discussion started by: jcdole
5 Replies

3. Shell Programming and Scripting

While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found,... (5 Replies)
Discussion started by: laknar
5 Replies

4. Shell Programming and Scripting

Variable not found error in while loop

I am unable to use the value of a variable. while ] do LstFldDataPart =`head -n "$LstFldDataPartCntr" "${FeedFileDir}/${FeedFileBadRecs}" | tail -1 | tr -d '\n'` echo $LstFldDataPart JndLstFldDataPart="${JndLstFldDataPart}${LstFldDataPart}" LstFldDataPartCntr=... (3 Replies)
Discussion started by: TomG
3 Replies

5. AIX

ksh file not found error for various files across system

Our AIX system is currently having an issue where calls to various executables are returning errors such as ksh: /usr/bin/oslevel: not found. This is even happening with system files, third party scripts, and with the full path included. Telnet connections are also closing immediately upon... (6 Replies)
Discussion started by: Chthonic
6 Replies

6. Shell Programming and Scripting

while loop error. (command not found)

can any1 please tell me what is problem with following code: i=1; cat test| while read CMD; do Var$i=$CMD; or Var$i=$(echo $CMD) ; let i++ doneI keep getting error : line 4: Var1=sometext: command not found (2 Replies)
Discussion started by: kashif.live
2 Replies

7. Shell Programming and Scripting

Search for file, give error if more than one file is found

Want to write a function that prints an error when passed a list of file names. If the file list is empty, print error "no file found", if there are more than one file, print "error more than one file found" (22 Replies)
Discussion started by: kristinu
22 Replies

8. Shell Programming and Scripting

For loop and file not found

Hi All, I have a simple script.sh file. It gives me " file.txt' for reading (no such file or directory)" Why can this happen? %ls ch1file.txt ch2file.txt ch3file.txt % chmod +x script.sh % bash % ./script.sh where script.sh is as follows for i in seq(3) do awk '{print $1}'... (1 Reply)
Discussion started by: senayasma
1 Replies

9. UNIX for Dummies Questions & Answers

Loop on array variable returning error: 0 not found

I'm guessing i have a syntax error. I'm not sure it get's past the the while condition. I get an error 0 not found. Simple loop not sure what I'm doing wrong. #!/usr/bin/ksh set -A MtPtArray /u03 /u06 tUbound=${#MtPtArray } echo $tUbound i=0 while ($i -lt $tUbound) do print... (4 Replies)
Discussion started by: KME
4 Replies

10. Programming

Error found while opening any pdf file from IE 6

Hello All, My application is followed J2ee architecture. It contains Java and jsp codes. This application generates some reports in pdf format. I am using Weblogic 10.3 and jdk 6. While I want to open the pdf in IE 6 with service pack 2 , the pdf not showing properly. It comes in byte code.... (3 Replies)
Discussion started by: priyankak
3 Replies
Login or Register to Ask a Question