[PLUG] filenames with spaces, shell programming ...

Tony Schlemmer aschlemm at comcast.net
Fri May 20 17:46:41 UTC 2005


On Friday 20 May 2005 11:41, Russell Senior wrote:
> I have got a file containing file names with spaces and exclamation
> marks, etc.  I want to, e.g., cmp -v two versions of these files,
> something like:
>
>   $ cmp -v $DIR1/$foo $DIR2/$foo
>
> When I stupidly do something like:
>
>   $ for foo in $(cat fnames) ; do echo $foo ; cmp -v $DIR1/$foo $DIR2/$foo
> ; done
>
> the filenames get split at spaces in the filenames.  How do I keep
> them from being split?
>
> When this issue has come up in the past, Randal usually pipes up with
> a solution that subtley implies that "you don't understand how this
> works".  I guess I need to improve my understanding at this stage.
>
> Any lucid explanation welcome.

I'm sure someone will come up with a more elegant solution but in this example 
I replace the spaces with a "#" character and then as each filename is 
processed, the "#" is translated back to a space character...Pretty lame I 
know...

#!/bin/sh
#
names=`sed -e 's/ /#/g' fnames`
for foo in $names; do
  fname=`echo "$foo" | tr "#" " "`
  echo "$fname";
done

Tony

-- 
Anthony Schlemmer
aschlemm at comcast.net



More information about the PLUG mailing list