simple scripting question - Powerpc

This is a discussion on simple scripting question - Powerpc ; I can't find an active group dealing with bash scripting and this is on a YD3.0 box so.... I'm trying to write a script to copy files into directories, the relevant bits are below, the problem is most of the ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: simple scripting question

  1. simple scripting question

    I can't find an active group dealing with bash scripting and this is on a
    YD3.0 box so....

    I'm trying to write a script to copy files into directories, the relevant
    bits are below, the problem is most of the directories contain spaces in the
    name (this is on a samba share).

    #!/bin/bash
    dirroot=/usr/local/content
    for dirname in 'find $dirroot -type d'
    do
    echo $dirname
    done

    The problem is when it hits a directory called "Porsche and Ferrari"
    $dirname gets set to Porsche. I know I'm missing something pretty obvious
    here I just need to pull the entire directory name including spaces into
    the $dirname variable, can anyone help ?




  2. Re: simple scripting question

    Draxen wrote:

    > I can't find an active group dealing with bash scripting and this is on a
    > YD3.0 box so....
    >
    > I'm trying to write a script to copy files into directories, the relevant
    > bits are below, the problem is most of the directories contain spaces in
    > the name (this is on a samba share).
    >
    > #!/bin/bash
    > dirroot=/usr/local/content
    > for dirname in 'find $dirroot -type d'
    > do
    > echo $dirname
    > done
    >
    > The problem is when it hits a directory called "Porsche and Ferrari"
    > $dirname gets set to Porsche. I know I'm missing something pretty obvious
    > here I just need to pull the entire directory name including spaces
    > into the $dirname variable, can anyone help ?


    How about using -exec?

    For instance:

    find /usr/local/content -type d -exec basename {} \;

    Suggested reading: http://www.oreilly.com/catalog/upt3/ (chapter 9)

  3. Re: simple scripting question

    whom wrote:

    > Draxen wrote:
    >
    >> I can't find an active group dealing with bash scripting and this is on a
    >> YD3.0 box so....
    >>
    >> I'm trying to write a script to copy files into directories, the relevant
    >> bits are below, the problem is most of the directories contain spaces in
    >> the name (this is on a samba share).
    >>
    >> #!/bin/bash
    >> dirroot=/usr/local/content
    >> for dirname in 'find $dirroot -type d'
    >> do
    >> echo $dirname
    >> done
    >>
    >> The problem is when it hits a directory called "Porsche and Ferrari"
    >> $dirname gets set to Porsche. I know I'm missing something pretty obvious
    >> here I just need to pull the entire directory name including spaces
    >> into the $dirname variable, can anyone help ?


    Normal for loop separators are whitespace. This is why you
    attempt a 'echo $dirname' at this point the for loop has interpreted
    the line to be:

    for dirname in Porsche and Ferrari

    that means that there are three variables - not the one that
    you anticipate. Quoting is your friend here. You can do

    "`find $dirroot -type -d`"

    which will capture everything returned for each entry within
    quotes so that any blanks won't cause you any problems.

    The solution below works well too - to solve your snippet, but
    since you are attempting to do more than just echo out the
    name, I'd recommend the extra quoting.

    > How about using -exec?
    >
    > For instance:
    >
    > find /usr/local/content -type d -exec basename {} \;
    >
    > Suggested reading: http://www.oreilly.com/catalog/upt3/ (chapter 9)


    jerry
    --
    // Jerry Heyman | "Software is the difference between
    // Amiga Forever :-) | hardware and reality"
    \\ // heymanj@acm.org | http://www.cs.stedwards.edu/~heyman
    \X/

+ Reply to Thread