PDA

View Full Version : Simple shell scripting problem: spaces in filenames...



mahy
July 28th, 2007, 09:07 AM
Hello,

i'd like to run a certain action for every file named "Root" within a defined directory tree. This is the stub:


#! /bin/tcsh

foreach filename (`find /mnt/win2/work/CVS/ETB_CRM_sources -name Root`)
echo $filename
end

The trouble is that some of the files've got spaces in their paths. Naturally, foreach breaks such paths into two (or more). How do i prevent it? THX for any help.

ssam
July 28th, 2007, 09:45 AM
there are some tips on http://wooledge.org/mywiki/BashPitfalls for similar problems. but i don't know if it will help when using find

girishv
July 28th, 2007, 11:57 AM
Hi,

why to write a script for it, why not just use the exec of find itself ?

find /mnt/win2/work/CVS/ETB_CRM_sources -name Root -exec echo {} \;

Do not forget the "{}" in the above command

Girish

mahy
July 28th, 2007, 05:59 PM
Ok, i fixed some problems this way:


#! /bin/tcsh

foreach filename (`find /mnt/win2/work/CVS/ETB_CRM_sources -name Root | tr \ \*`)
set newname = `echo $filename | tr \* \ `
echo "certain string" > $newname
end

But but the last echo is giving me an error output, due to some ambiguity (wtf?!). How do i fill every "Root" file with a desired content?

angustia
July 28th, 2007, 06:07 PM
try with:

echo "$filename"

mahy
July 28th, 2007, 07:14 PM
try with:

echo "$filename"

Thanks! The quotation marks fixed it!