Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 28th, 2006, 1:48 PM   #1
MegaArcon
Programmer
 
MegaArcon's Avatar
 
Join Date: Aug 2005
Posts: 66
Rep Power: 0 MegaArcon is an unknown quantity at this point
String Substiution. Whitespace?

Hi all!

I've got a little bash script that basically takes my directory path to the "My Documents" folder. Instead of the space, I've inserted a special sequence that I want replaced with "\ " so that I can cd into that directory. However, I get an interesting result.

Here's the Script:
#!/bin/bash
MYDIR=/users/cs/study/gordon/MyxyXYWhitespacEYXyxDocuments
MYDIR=${MYDIR//xyXYWhitespacEYXyx/\\\ }
echo $MYDIR
cd $MYDIR
ls

Here's the output when I run my file:

/users/cs/study/gordon/My\ Documents
./test: line 5: cd: /users/cs/study/gordon/My\: No such file or directory

So the echo shows that the "\ " was put into the variable, but when I try and cd into it, it cuts off at the "My\". Does the string substitution put in something unique instead of just a whitespace? Am I using this correctly?

Thanks in advance for any insight.
__________________
Isn't that just the way life goes? If it's worth doing, it's NP-Hard. Todd Wareham - Memorial University of Newfoundland
MegaArcon is offline   Reply With Quote
Old Jun 28th, 2006, 2:00 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Try this:

#!/bin/bash
MYDIR="./My Dir"
echo $MYDIR
cd "$MYDIR"
ls


Was there a particular reason that you were avoiding the use of the space in the directory name? If not, this will work out for you.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jun 28th, 2006, 2:15 PM   #3
MegaArcon
Programmer
 
MegaArcon's Avatar
 
Join Date: Aug 2005
Posts: 66
Rep Power: 0 MegaArcon is an unknown quantity at this point
Ah ha! The quotes MegaArcon! The quotes! Why thank you Infinite Recursion. Looking at your code helped to spark what I was missing. Instead of subbing in "\ " I can just sub in " " and surround my variable with double quotes for the cd.

New code:
#!/bin/bash
MYDIR=/users/cs/study/gordon/MyxyXYWhitespacEYXyxDocuments
MYDIR=${MYDIR//xyXYWhitespacEYXyx/ }
echo $MYDIR
cd "$MYDIR"
ls

New output:
/users/cs/study/gordon/My Documents
Desktop    My Pictures        foo.doc

Ah, good ol' bash. Thanks again.
__________________
Isn't that just the way life goes? If it's worth doing, it's NP-Hard. Todd Wareham - Memorial University of Newfoundland
MegaArcon is offline   Reply With Quote
Old Jun 28th, 2006, 2:47 PM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
You're welcome
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC