Register
It is currently Wed Oct 22, 2014 5:04 pm

Need help with escape charactered file list in a for loop


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Sun Sep 11, 2011 9:49 am   

Joined: Tue Jan 19, 2010 9:10 pm
Posts: 12
No matter what I do I can't get a file list that includes spaces to save right in a variable.


Here is my script so far

Code:
#!/bin/bash
cd ~/movies

#####two different options that have the commands work on command line but not saved to the variable right. Output of both is the full path to each file from the current $PWD with escape characters
#for i in `ls -1Rb | sed /^$/d | gawk '/:$/{dir=substr($0,1,length($0)-1)} !/:$/{printf "%s/%s\n",dir,$0}' | grep .nfo`
for i in `find . -type f | sed 's/[!@\ "()]/\\&/g' | grep .nfo`

do
##### What I plan on actually doing just commented out till the variable is right
#grep "<id>tt" '$i' | sed s/\ \ \ \ \<id\>tt/http:\\/\\/www.imdb.com\\/title\\/tt/ | sed s/\<\\/id\>// >> '$i'

##### my quick test for if the variable saves the file list right
echo $i
done



Top
 Profile  
 PostPosted: Mon Sep 12, 2011 1:48 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
hi,

don't use
Code:
for i in $(any command); do
it'll iterate on each element `any command` writes to stdout.
instead use bash globs :
Code:
for f in ./*.nfo
do
# for example
   echo "$f" #don't forget quotes !
done


for more please show us what your hierarchy looks like.


Top
 Profile  
 PostPosted: Mon Sep 12, 2011 7:55 am   

Joined: Tue Jan 19, 2010 9:10 pm
Posts: 12
My directory structure is not the cleanest and can include special characters and spaces in file names and directories which is what is causing most of my problems.

in general with some exception it is setup as movies/firstletterorgeneraltrilogyname/moviefiles

I need it to be a recursive search for nfo files and have the full path relative to $PWD


Top
 Profile  
 PostPosted: Mon Sep 12, 2011 9:21 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Tried using 'find' yet? I'm not *quite* clear on what the substitution is for at a glance, but does this help you get any closer?

Code:
find . -name '*.nfo' -exec echo "{}" \;


Hope this helps!


Top
 Profile YIM  
 PostPosted: Mon Sep 12, 2011 9:29 am   

Joined: Tue Jan 19, 2010 9:10 pm
Posts: 12
Thanks with that command I have a working script.


Top
 Profile  
 PostPosted: Mon Sep 12, 2011 10:09 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
awesome! glad we could help :)


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP