David the H. |
09-26-2010 04:02 AM |
Either way does the same thing in the end, really, although the while-read loop seems to be much more popular for some reason. A for loop reads the input a "word" at a time, while read grabs whole lines by default.
Whichever one you use, you should first make sure that input list is being generated correctly, then ensure that the loop is processing them properly. I usually do this by simply echoing the variable back, like this:
Code:
while read x; do
echo "$x" | cat -A
done < <( list_generating_command ) #or <file
Piping things through cat -A is useful in cases where there's a chance of unwanted trailing spaces or non-printing characters appearing in the field. It can help you to spot dos-style carriage returns when working on text files that may have been produced on a Windows machine, for example, or where tabs have been used instead of spaces.
Once you know the basic loop is working, you can replace it with the commands you want.
PS: Don't forget that find also has an -exec option, which is useful when you only have to run a single command on the files it finds.
|