Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   String Substiution. Whitespace? (http://www.programmingforums.org/showthread.php?t=10557)

MegaArcon Jun 28th, 2006 1:48 PM

String Substiution. Whitespace?
 
Hi all! :D

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. :D

Infinite Recursion Jun 28th, 2006 2:00 PM

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.

MegaArcon Jun 28th, 2006 2:15 PM

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. :D

Infinite Recursion Jun 28th, 2006 2:47 PM

You're welcome :)


All times are GMT -5. The time now is 1:47 AM.

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