nested while loop keep looping when finished
I have a nested while loop (2). so this will go through a parent dir and get the children dirs one at a time then (basically) moving files somewhere else then when finished moving the files, delete prior dir and sub-dir without removing the parent dir before moving on to the next child dir and doing the same. But it just keeps looping until it gets tired then moves on to the next one.
it is at "big" script. do not let that confuse you. as stated it is after processing the files it is just moving them somewhere else then deleting the prior dir structure, leaving the parent. quick break down: it reads from one dir, process files, copies file to one dir then moves the original file to another dir to have a back up copy. then deletes the dirs they use to be in. then it just keeps looping until "it gets tried or dizzy" before moving on. script Code:
#!/bin/bash Code:
Code:
How do I get that to stop after it deletes the dir just have it move on to the next. |
Hi,
First off, I only quickly glimpsed at your script, and this is what stood out... your issue is that parenthesis create a subshell, meaning that your "break" statement gets executed in a completely different shell (same issue with exit, it exits a completely different process than you think). I think that replacing the parenthesis around the break with curly braces will fix your issue. In any case, the following works : Code:
i=0 |
Quote:
It should not even need a break in that loop, I've changed it to a basic if statement and am getting the same results. Next to try is the mindepth and maxdepth trial. because this is making no since to me. When find is done with one directory it is suppose to move on to the next one. Not keep looking inside of it, especially when it is not there. I could just run it and not worry about it, because it works in what it is suppose to be doing. Tough it is slightly bugging me as to why it is not doing what I expect it to when finished and dir is deleted. |
moved code to outside of both loops so it will just delete everything within the dir when finished completely running. Instead of one at a time.
Code:
done< <(find "$path" -type f \( -name "*.mp3" -o -name "*.MP3" -o -name "*.flac" \)) |
Quote:
see man bash: Code:
(list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Quote:
Quote:
you posted incomplete scripts, hard to say anything about that, but: you may use -iname in find, probably a bit better. |
Quote:
Quote:
it has to be something within the find function that it must be keeping a list within itself, and then using that to still search the dir and sub dir until it sees they are not there. hence it goes though them one at a time if sub directories are present within its search list. Quote:
Quote:
|
find collects the information before starting the loop and did not recognize you removed something during execution. probably -depth can help on this, but not really sure.
|
Quote:
Like I stated it works, only that find will keep running not finding anything, so it has nothing to act on, so all that is really taking place is time going by until it gets to the next directory that it can work on is all. |
Both loops are fed from a redirected stdout to stdin:
Code:
while Or... it's the variables that get overwritten; you read into FILENAME variable in both loops; As far as I could tell in the glimpse I took, they are both in the same (sub)shell; hence in the same scope: hence they're overwritten. |
Quote:
I'll give that a try. thanks. |
Yes, the FILENAME variable is shared.
This is because each while loop is not fed from a pipe find ... | while read ... that would force a sub shell. Looks risky, but should work here nevertheless, because it is set at the beginning of the loop to the correct value (from the loop's dedicated input stream). But the whole structure is fishy somehow. Certainly needs some cleanup. Why two nested loops at all? find does a recursion already - shouldn't a simple loop go through the hierarchy? |
Here's a couple of suggestions:
(1) "Use a real programming language." Pick any one you like, then use Bash's #!"shebang" feature to specify which language Bash should invoke to run your script. (2) In general, I do not recommend altering any directory structure while you are also in the process of running it. Run it first, placing a list of all matching entries into an array. Then, process the array content. Alternate between "building that list" and "consuming it." (2a) Most "real" programming languages have a ready-to-use directory runner. |
All times are GMT -5. The time now is 02:55 AM. |