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 ...
- 01-08-2008 #1
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:
This works FINE until you get to a directory with a space in the name...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`";
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?
- 01-08-2008 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Try to quote the variable:
Or change the field seperator:Code:pushd "$k" 1>/dev/null
RegardsCode:OLDIFS=$IFS Do your stuff here.... IFS=$OLDIFS
- 01-08-2008 #3
Wow head into wall... all I had to do was quote the $k...
THANKS!
- 01-08-2008 #4
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.
- 01-08-2008 #5Head onto keyboard is less likely to risk serious injury.Wow head into wall--
Bill
Old age and treachery will overcome youth and skill.
- 01-08-2008 #6Linux Engineer
- Join Date
- Nov 2004
- Location
- Ft. Polk, LA
- Posts
- 796
- 01-08-2008 #7Priced keyboards recently?a damaged keyboard is a bigger loss than a damaged wall
Priced wallboard recently?
Just sayin'. :)--
Bill
Old age and treachery will overcome youth and skill.
- 01-12-2008 #8Linux 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!
- 01-12-2008 #9Cheaper than wallboard, too.Space are separators!--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote
