![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Aug 2005
Posts: 66
Rep Power: 0
![]() |
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
lsHere'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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Aug 2005
Posts: 66
Rep Power: 0
![]() |
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"
lsNew 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 |
|
|
|
|
|
#4 |
|
Programming Guru
![]() ![]() ![]() |
You're welcome
![]()
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|