LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 05-21-2006, 06:44 PM   #1
RijilV
Member
 
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: 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...
 
Old 05-21-2006, 07:57 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,052

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
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
 
Old 05-21-2006, 08:09 PM   #3
eglnx
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, 08:29 PM   #4
RijilV
Member
 
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.
 
  


Reply

Tags
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 03:57 AM
bash script works when interactive, endless loop when started via cron dguy Linux - General 5 04-10-2006 11:39 AM
Loop Bash command to hit... rumperpumper Linux - General 4 01-31-2006 08:39 PM
for loop only works properly on first loop symo0009 Programming 1 12-25-2005 05:17 PM
for loop inside select box opioid Programming 1 03-17-2005 02:22 PM


All times are GMT -5. The time now is 01:30 PM.

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