Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Shell scripting, repeat with files, space in names (NEED HELP PLEASE)

Shell scripting, repeat with files, space in names (NEED HELP PLEASE)
Thread Tools
Junior Member
Join Date: Sep 2000
Location: Milwaukee, WI
Status: Offline
Reply With Quote
Apr 21, 2004, 09:50 PM
 
#!/bin/sh

files=`find -d '/Users/admin/Desktop/space space' -iname '*.webloc'`;

for i in $files; do
echo "$i";
done;


How do I allow the repeat loop to treat paths with spaces as one item?
     
Dedicated MacNNer
Join Date: Sep 2000
Location: New York, NY
Status: Offline
Reply With Quote
Apr 22, 2004, 06:39 AM
 
I'm not sure if Bourne shell has this feature, but Korn shell (now available as open source), and I presume pdksh use an environment variable called $IFS for Internal Field Separator. By default, it's set to space,tab,and newline. You could use ksh and reset this variable

Code:
#!/usr/bin/ksh # remember IFS value so you can reset it old_IFS=$IFS # change IFS to just newline IFS="\n" for i in $files do echo $i done # reset IFS IFS=$old_IFS
Rather than getting into ksh, you could do the same in Perl:

Code:
#!/usr/bin/perl @files=`find ~/Desktop -iname "*.webloc"`; foreach $file (@files) { print "$file"; }
     
Mac Elite
Join Date: Sep 2000
Location: Edmond, OK USA
Status: Offline
Reply With Quote
Apr 22, 2004, 09:53 AM
 
The cheapy quick answer is to temporarily move the folder to space_space, run the command, then move it back. There must be some sort of way to quote the names in the list?

As a second step you could use the -exec option of find to do your file manipulation from there. That way you wouldn't need the files variable.
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Apr 22, 2004, 06:01 PM
 
Can't you just escape space characters with a "\"? Ie, /blah/blah/space\ space
?
     
n8910  (op)
Junior Member
Join Date: Sep 2000
Location: Milwaukee, WI
Status: Offline
Reply With Quote
Apr 22, 2004, 10:22 PM
 
Suggestion to modify IFS was spot on. Thank you.

#!/bin/sh
files=`find -d '/Users/admin/Desktop/space space' -iname '*.webloc'`;
old_IFS=$IFS
IFS='
'
for i in $files
do
echo $i
done
IFS=$old_IFS

Interestingly enough IFS="\n" used n as a delimiter - makes sense..

Perl solution was known at the time, this is more an exercise in learning sh.

For anyone else who is curious, IFS by default is space tab return.

Thanks again, I really appreciate the help.


Nate
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:10 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2