Find the answer to your Linux question:
Results 1 to 9 of 9
So I'm messing around with my OLPC, finally learning linux because I've put it off so long, and I'm getting into the bash scripting to do things I'd rather not ...
  1. #1
    Just Joined! unrequited's Avatar
    Join Date
    Jan 2008
    Posts
    2

    bash scripting -- popd with directories containing spaces

    So I'm messing around with my OLPC, finally learning linux because I've put it off so long, and I'm getting into the bash scripting to do things I'd rather not repeat over and over. Here's my question, how do I pass directory strings to push/popd that have spaces in them? Here's a basic script I want to flesh out later to run a command in each directory under a current tree:

    Code:
    #!/bin/bash
    
    echo "starting craps, now in directory: `pwd`";
    
    tree -idf --noreport |while read k;
    * * do
    * * * * echo "pushing $k to dirstack";
    * * * * pushd $k 1>/dev/null;
    * * * * echo "executing b.s. command in `pwd`";
    * * * * popd 1>/dev/null;
    * * done;
    
    echo "done, now in directory: `pwd`";
    This works FINE until you get to a directory with a space in the name...

    I've tried piping the tree command to sed to try and escape the spaces:
    |sed 's/ /\\\\ /g'

    and even piping all that into sed again to encapsulate it all in single quotes:
    |sed 's/.*/'\''&'\''\//g'

    but I'm stumped.

    From the prompt itself, you can certainly do a:
    popd ./directory\ with\ spaces
    and it will work just fine and if you want, you can scrap the space escaping and go straight to single quotes:
    popd './directory with spaces'

    but neither will work in a bash shell script passed to popd.

    Can anybody help?

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try to quote the variable:

    Code:
    pushd "$k" 1>/dev/null
    Or change the field seperator:

    Code:
    OLDIFS=$IFS
    
    Do your stuff here....
    
    IFS=$OLDIFS
    Regards

  3. #3
    Just Joined! unrequited's Avatar
    Join Date
    Jan 2008
    Posts
    2
    Wow head into wall... all I had to do was quote the $k...

    THANKS!

  4. #4
    Super Moderator MikeTbob's Avatar
    Join Date
    Apr 2006
    Location
    Texas
    Posts
    7,144
    Hi unrequited,
    Welcome to the forums, glad you got your problem fixed. I was wondering about the OLPC itself, if you don't mind me asking a few questions. Did you get it through the b1g1 program? Why did you decide to acquire an OLPC? Is it a pretty good machine (as advertised)? Are you able to use the Mesh network?
    I do not respond to private messages asking for Linux help, Please keep it on the forums only.
    All new users please read this.** Forum FAQS. ** Adopt an unanswered post.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Wow head into wall
    Head onto keyboard is less likely to risk serious injury.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Ft. Polk, LA
    Posts
    796
    Quote Originally Posted by wje_lf View Post
    Head onto keyboard is less likely to risk serious injury.
    But a damaged keyboard is a bigger loss than a damaged wall!

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    a damaged keyboard is a bigger loss than a damaged wall
    Priced keyboards recently?

    Priced wallboard recently?

    Just sayin'. :)
    --
    Bill

    Old age and treachery will overcome youth and skill.

  8. #8
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    First Windows habit to break when using Linux: don't put spaces into file or directory names. Space are separators!

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Space are separators!
    Cheaper than wallboard, too.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...