Sunday 15 May 2011

Problem looping files under UNIX

I was writing some script under korn shell which suppose to loop X directory and search for the files with “prog” extension. The script is very simple

for file in "*.prog"
do
   echo $file
done;

 


Just to clarify, where was no prog files in this directory while I executed it!!! But, the result of the script was


*prog


I was shocked!!. I tried to execute the same script under “bash" and result were the same. For some reason UNIX was thinking that I do have “prog” file in my directory, but I didn’t. Finally I found how to do it safely. Use the following piece of code





for file in `ls *.prog 2> /dev/null`
do  
   echo $file
done;

This one worked just fine.


If any one knows what is the reason for this strange behavior , please comment it

No comments:

Post a Comment