LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash: command works but not inside of for loop. (https://www.linuxquestions.org/questions/programming-9/bash-command-works-but-not-inside-of-for-loop-447168/)

RijilV 05-21-2006 07:44 PM

Bash: command works but not inside of for loop.
 
Hey, I have a bash script: http://riji.lv/hiro.txt
In there I run the command:
Code:

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:
Code:

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

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

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:

Code:

#! /bin/sh

set -f

FCL=" -iname *.pdf -print -o -iname *.html -print "
dir="/tmp"


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

It gives this output:
Quote:

file in loop: /tmp/no.video.html
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:
Quote:

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

jlinkels

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.