Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-21-2006, 07:44 PM   #1
Registered: Sep 2002
Location: somewhere
Distribution: gentoo
Posts: 123

Rep: Reputation: 15
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...
Old 05-21-2006, 08:57 PM   #2
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,187

Rep: Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037
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?

Old 05-21-2006, 09:09 PM   #3
LQ Newbie
Registered: Mar 2005
Posts: 10

Rep: Reputation: 0
Leave IFS at the default value and your code will work.
Old 05-21-2006, 09:29 PM   #4
Registered: Sep 2002
Location: somewhere
Distribution: gentoo
Posts: 123

Original Poster
Rep: Reputation: 15
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.


bash, programming, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
works on command line but not in bash script tara Linux - General 7 02-09-2009 04:57 AM
bash script works when interactive, endless loop when started via cron dguy Linux - General 5 04-10-2006 12:39 PM
Loop Bash command to hit... rumperpumper Linux - General 4 01-31-2006 09:39 PM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 06:17 PM
for loop inside select box opioid Programming 1 03-17-2005 03:22 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:50 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration