-   Programming (
-   -   Bash: command works but not inside of for loop. (

RijilV 05-21-2006 07:44 PM

Bash: command works but not inside of for loop.
Hey, I have a bash script:
In there I run the command:

find $dir -type -f $FCL
where "$FCL" is something like "( -iname *.foo -print -o -iname *.bar -print )"

This works fine if I assign it to a variable, such as:

fList=`find $dir -type f $FCL`
however when I try this in a for loop such as:

for file in $( find $dir -type -f $FCL );
I get:

find: invalid predicate `( -iname *.foo -print -o -iname *.bar -print )'
I have no idea why this is happening, I've attempted a few things, putting things in quotes, writing the variables like ${FCL} etc.. nothing seems to matter. Any ideas welcome...

jlinkels 05-21-2006 08:57 PM

For me this worked:


#! /bin/sh

set -f

FCL=" -iname *.pdf -print -o -iname *.html -print "

for file in $( find $dir -type f $FCL )
        echo "file in loop: $file"

It gives this output:

file in loop: /tmp/
file in loop: /tmp/weg.pdf
file in loop: /tmp/X3e_61873972.pdf

Which is as expected.

Maybe you can develop this to your piece of code...

BTW, in you post you write:

for file in $( find $dir -type -f $FCL );
Is the "minus f" a typo which does not occur in your real code?


eglnx 05-21-2006 09:09 PM

Leave IFS at the default value and your code will work.

RijilV 05-21-2006 09:29 PM

If I don't change the IFS how can I handle files with spaces in them?

But thanks for pointing that out to me, gives me some idea how to solve this.

All times are GMT -5. The time now is 12:43 AM.