Re: bullet proof for loop over $(find) in bash, how?

From: Stephane Chazelas (stephane_chazelas_at_yahoo.fr)
Date: 11/24/05


Date: 24 Nov 2005 09:40:59 GMT

On Wed, 23 Nov 2005 21:05:19 GMT, Harald wrote:
> Stephane Chazelas <stephane_chazelas@yahoo.fr> writes:
>
>> On 23 Nov 2005 08:44:39 -0800, HK wrote:
>>> In an idiom like
>>>
>>> for x in $(find ...); do something $x; done
>>>
>>> how can I make sure that I execute "something" exactly for
>>> each file that is found by find, even if the file names contain
>>> blanks, newlines or tabs?
>
>> eval set --"$(
>> find .//. ... -print |
>> awk "
>> function escape(s) {
>> gsub(/'/, \"'\\\\\\''\", s)
>> return \"'\" s \"'"'"
>> }
> [snip]
>
> Huuuuh, I was afraid of something like that. At least the ".//." is a
> nice idea.
>
>> Chances are that if you need an array in a script, then you need
>> a real programming language.
>
> Not really. The shell is (nearly) perfect as glue to start and
> manipulate other processes. I don't know of any programming language
> were you can do this with the same ease. I would use tclsh (note, not
> tcsh), but its exec is broken in that it cannot pass '<' as a command
> line argument.

But as the filenames start with "./", that won't be a problem.

>> Note that arrays are not standard
>> features of shells and are generally a feature neither portable
>
> I agree. The nice thing about arrays is that you know exactly what
>
> for x in "${ary[@]}" ...
>
> loops over, independent of any special characters contained in any of
> the array elements.
>
> How can this be done without array: get arbitrary strings, one at a
> time and store them in a variable mylist such that later a loop like
>
> for x in $mylist ...
>
> loops exactly over the strings. I guess it requires some quoting and
> eval.
[...]

Typically, because in ksh clones or half clones (such as bash),
arrays are only good in that case, they are not necessary. The
Bourne arrays ("$@") are enough.

Instead of:
array=(...)
(which doesn't work reliably in bash2, BTW)
use
set -- ...

Looping over them is just:

for i do ...; done

instead of that ugly "${array[@]}"

-- 
Stephane


Relevant Pages

  • Re: bullet proof for loop over $(find) in bash, how?
    ... the array elements. ... How can this be done without array: get arbitrary strings, ... loops exactly over the strings. ... Harald Kirsch | ...
    (comp.unix.shell)
  • Re: Global array operations: a performance hit?
    ... as if many DO loops were executed instead than just one. ... global array operations then? ... Note that the usual terminology is something more like "whole array ... once in a while they might also get you faster execution, ...
    (comp.lang.fortran)
  • Re: Puppy Mastiff wants to Nip at Faces
    ... first couple of weeks of an introductory data structures ... it seems to me by my recollection that loops were... ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily access ...
    (rec.pets.dogs.behavior)
  • Re: Global array operations: a performance hit?
    ... when using array operations you have additional information ... which is not always the case in DO loops. ... optimizing array expressions. ... The compiler has to ...
    (comp.lang.fortran)
  • Re: Inefficient code?
    ... line is written to the output file. ... or read it into an array. ... # Backup File Field Positions ... Define 'my %found;' above the loops. ...
    (perl.beginners)