Results 1 to 3 of 3

Thread: Some help scripting (blank space in variable)

  1. #1

    Some help scripting (blank space in variable)

    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)



    Code:
    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 !

  2. #2
    Join Date
    Jun 2005
    Location
    Slovenia
    Posts
    736
    Code:
    cd "$sSource/$sDir"

  3. #3
    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 !

Similar Threads

  1. Replies: 4
    Last Post: 26-10-2007, 00:26
  2. blank page
    By ZPapp23 in forum WL-500gP Firmware Discussion
    Replies: 0
    Last Post: 28-07-2007, 20:53
  3. System wide variable (how to?)
    By scriptman in forum WL-500gP Q&A
    Replies: 4
    Last Post: 27-11-2006, 00:00
  4. Scripting or automatic disable Radio
    By airone in forum WL-500g Q&A
    Replies: 2
    Last Post: 15-12-2005, 15:39

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •