Re: bullet proof for loop over $(find) in bash, how?
From: Stephane Chazelas (stephane_chazelas_at_yahoo.fr)
Date: 11/24/05
- Next message: serrand: "Re: how to rename set of files"
- Previous message: Petterson Mikael: "help using find on linux"
- In reply to: Harald: "Re: bullet proof for loop over $(find) in bash, how?"
- Next in thread: dan.rickhoff_at_comcast.net: "Re: bullet proof for loop over $(find) in bash, how?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: serrand: "Re: how to rename set of files"
- Previous message: Petterson Mikael: "help using find on linux"
- In reply to: Harald: "Re: bullet proof for loop over $(find) in bash, how?"
- Next in thread: dan.rickhoff_at_comcast.net: "Re: bullet proof for loop over $(find) in bash, how?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|