euglug.org

[Eug-lug] Shell scripting IFS woes

작성자

카테고리:



Fred JamesMon Nov 14 20:23:57 PST 2005

Patrick R. Wade wrote:

> My problem ; i have a file containing a list of filenames, which may 
> contain spaces or dots in them:
> ---
> foo
> bar baz
> quux.random
> ---
> I would like to execute a series of scp commands on them:
> ---
> scp me at there:foo here/
> scp "me at there:bar baz" here/
> scp me at there:quux.random here/
> ---
> My various efforts at IFS definition have not done what i wanted.
> Can anyone point me towards the right way to do this?
>
> Thanks for your time...
>
Patrick R. Wade
Would something like this help?
Regards
Fred James

#! /bin/bash
#       fubar.sh
#       to preform scp commands on the files listed in fubar
#
OrigFile="fubar"
TempFile="Fubar"
cp /dev/null $TempFile
while read fileName
do
        echo $fileName | awk '{
                N = split($0, FUBAR, " ")
                for (i = 1; i <= N; i++) {
                        if (i == 1) {
                                Name = FUBAR[i]
                        } else {
                                Name = Name "\\ " FUBAR[i]
                        }
                }
                printf ( "scp \"%s\" \"%s.new\"\n", Name, Name )
        }' >> $TempFile
done < $OrigFile
sh $TempFile
exit