PDA

Bekijk de volledige versie : Some help scripting (blank space in variable)



raas
17-11-2007, 15:19
Hi,

writing a script here, but I have some problems.

the script has a loop where it scans directories in a dir.
Works fine, only problem is that when the dir has a space in the name I can't 'cd' to it within the .sh script.
I've tried various things, but I haven't managed to fix this.

Here goes:
(I've cut the part of the script where the error occurs.)

I have this dir: '/mnt/usenet_temp'
It contains 2 dirs: '2 2' & '3 3' (which contain rar packages, but that's not the issue here)





sSource='/mnt/usenet_temp'
# set working dir to the source dir
cd $sSource

# get the contents of the source dir in to a variable
# -p is used to append a / to the contents after each dir.
# this is for easier cutting
sSourceDirContents=$(dir -p)

# because sSourceDirContents contains more than 1 dir, only
# cut the first dir into variable sDir
sDir=`echo $sSourceDirContents | cut -d / -f 1`


# now I let the contents display to see what I've got
# contents would display: /mnt/usenet_temp/2\ 2
# if I 'cd' this manually I get to the dir
# this script can't 'cd' to it. command gets cut of after \
echo $sSource/$sDir
cd $sSource/$sDir
echo $(pwd)


# If I let this script do a 'static' 'cd' instead of a dynamic such as above
# it works !! I end up in the correct dir.
echo /mnt/usenet_temp/3\ 3
cd /mnt/usenet_temp/3\ 3
echo $(pwd)


As you can see I've put some comments in there which are normally not there.
First I build a string dynamically and try to use it to 'cd' to a dir, which does not work, while when I copy the output of this dynamic string and use that to 'cd' it works.
Also when I use a static 'cd' command in the script, it goes to the right dir.

does anybody have an solution how to dynamically 'cd' to a dir which contains a 'space' character ?

Thanks a lot !

oleo
17-11-2007, 15:25
cd "$sSource/$sDir"

raas
18-11-2007, 16:32
Hi Oleo,

Thanks for your reply.
I've tried that, but that also didn't work.
However, with the code you mentioned the path is passed through correctly, it is not cut off after the first 'space'
But the 'cd' command does not accept this: it returns: can't cd to /mnt/usenet_temp/2\ 2

however, if I copy that exact string and manually cd it, it works.

I checked if there where any trailing spaces or something, but this was not the case.

Any ideas on this?

Thanks !