How the for loop works?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How the for loop works?
# 1  
Old 02-04-2007
How the for loop works?

Hi,
I have a landing area where some files keep on coming after irregular intervals of time. From this landing area, I need to move files to another directory for processing. For this, I am using a for loop to find certain kinds of files in the landing area.

Now my question is, suppose I start my script which moves the files at a certain moment and by the time the script completes, more files come into the landing area.... will the new files be moved into the processing directory too??

According to me it should not....but how exactly does the for loop work? how does it know what files to take?

Thanks
Neelaksh
# 2  
Old 02-04-2007
They are not.

for VAR in SOMETHING

SOMETHING is only evaluated ONCE at the beginning of the for loop.

So if if SOMETHING would select a number of files, then only those files which meet the selection at the time the for statement is executed will be taken into consideration.

The for has no knowledge of files which "arrive" at a later time and would have met the condition if they had been present at the time the for is executed.
# 3  
Old 02-04-2007
okk.. I thought so too....what happens in this scenario...

I am talking about close to 15,000 files totalling about 40 Gb in size. What if during the execution of the for statement, new files are added into the directory???
# 4  
Old 02-04-2007
The number and total size of the files is irrelevant.

All new files will not be affected by whatever happens inside the for loop.

If you want them to be affected as well you could put the for loop e.g. inside a while loop.

Suppose your for loop works on files with the extention ".txt".

while [ `ls *.txt > /dev/null 2>&1; echo $?` -eq 0 ]
do
for FILE in `ls *.txt`
do
do_whatever_needs_to_be_done
done
done

The while loop makes sure that as long as *.txt files are present in the directory the for loop will be executed over and over.

Ofcourse this process will come to an end at sometime as well, when no more *.txt are present. New *.txt files arriving shortly after are not affected either.
# 5  
Old 02-04-2007
1.) Is there any possibility that the snapshot goes wrong?? by snapshot i mean the list of files in the directory at the time of execution?

2.) Suppose i execute the script, the for loop evaluates the 'ls *.dat' command.... and while it is moving those files... i delete a few of them... will the script thrown an error??
# 6  
Old 02-04-2007
Like the "for" has no knowledge of files being added it also has no knowledge of files being removed.

So if at the time of evaluating the "for" some files are present, which are later on removed, the "for" will still try to perform the actions which are mentioned inide the loop. If the files are gone this will result in error messages.
# 7  
Old 02-04-2007
okk... thanks for answering my queries Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

"Mv" command does not work in loop, but works manually

Hi there, this may be a beginner's error, but I've been unable to find a solution on my own and by googling, and now I am really stuck on it. I am simply trying to move directories called for example CAT_Run01.ica to a directory with the corresponding number, Run01, in the same directory. For... (2 Replies)
Discussion started by: andrevol
2 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

[ask]how does sed -e 's/<[^>]*>//g' works?

I found this command, sed -e 's/<*>//g', will return the content of a line with pattern something like this, <tag1>content</tag1>.. How does this works? What does sed -e 's/<*>//g' actually do? What if I wanted to get content of a line with pattern something like this, content? thanks.. (5 Replies)
Discussion started by: 14th
5 Replies

4. UNIX for Advanced & Expert Users

How this works?

I have a program............ #include<stdio.h> #include<unistd.h> main() { if(fork == 0) { printf("Hi every body:p!!!!!!!!!!"); } } This program works with out any error. here fork is not a system call. It just act as a variable.But how it works without declaring it? What data type it... (19 Replies)
Discussion started by: carolsanjeevi
19 Replies

5. UNIX for Dummies Questions & Answers

>./a.pl works, >a.pl - does not

When I try to execute script, I get message: >aa.pl zsh: command not found: aa.pl but >./aa.pl works OK. What to change in environment to force the former way to work? Thank you, Alex Z (4 Replies)
Discussion started by: zzol
4 Replies

6. Solaris

how inode works

HI, Just another dummies questions: How i can determine what number of inode to use when creating filesystem? Thanks (4 Replies)
Discussion started by: lamoul
4 Replies

7. Programming

how this works?

pls explain me how this works.... DECODE (SUBSTR (field, 1, 1),'''', '''''' || field || '''','''' || field || '''') here field is a column in an oracle table.... (7 Replies)
Discussion started by: vijay_0209
7 Replies

8. Shell Programming and Scripting

Still trying to get a grep -c that works

I am trying to get a count of each line sub runit2 { my ($file1a, $file2a) = @_; my $file1_vala = $file1a->get; my $file2_vala = $file2a->get; open (FILE1a, "$file1_vala") or die; open (FILE2a, "$file2_vala") or die; chomp(my @strings = <FILE2a>); while (1) { foreach $pattern... (4 Replies)
Discussion started by: popeye
4 Replies

9. UNIX for Dummies Questions & Answers

How ls | wc -l works?

ls displays files in tabbed output. Say a directory contains 3 files. ls will list all 3 in one line. So, I expect ls | wc -l to give 1, but it counts the nr of files and gives 3. Can someone explain how this works? (3 Replies)
Discussion started by: krishmaths
3 Replies

10. UNIX for Dummies Questions & Answers

how sendmail works

Excuse me for this question really for dummies! I would like to know how sendmail works, obviously even in few words. If it uses a mail server or relay to send mail, if there is some check that sendmail makes to the from address and so on... Thank you very much. (3 Replies)
Discussion started by: alzep
3 Replies
Login or Register to Ask a Question