Find the answer to your Linux question:
Results 1 to 3 of 3
Hello forums! I've been tinkering with a shell script to partition and restore content to a drive based on a type of file in a given directory. My goal is ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    15

    Whitespace issue

    Hello forums!

    I've been tinkering with a shell script to partition and restore content to a drive based on a type of file in a given directory. My goal is for my script to assemble several restore images, partition the drive based on the images and to then restore those images to the partitions on the drive. Its going to be a multi-boot drive for troubleshooting different systems. Everything is going fine so long as the path to my "configuration" directory does not contain whitespaces.

    Here is a snippet:

    Code:
    for file in `ls "/test folder"/*.ext`; do echo "$file"; done
    The result is unusable:

    Code:
    /test
    folder/test1.ext
    /test
    folder/test2.ext
    /test
    folder/test3.ext
    I've tried single and double quoting all over the place and I can't find the correct usage. All I'm looking for is this:

    Code:
    /test folder/test1.ext
    /test folder/test2.ext
    /test folder/test3.ext
    The "configuration" folder in my actual script is a variable passed on from another call earlier in the script. I know I could use:
    Code:
    for file in `ls /test\ folder/*.ext`; do echo "$file"; done
    but because the "/test folder" is a variable grepped from the output of a function that only gives human readable output, I'm stuck.


    Thanks for any help - I've been working on this line of my script for 3 days now and I can't figure it out. I'm always in awe of how well some folks have mastered the command line. Thanks again!

    - robbie -

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So actually, all that your quoting is going to do is mess with the way that find handles the space, and that is actually fine. The real problem here is the way that for loops handle their input. By default, Bash splits on any sort of whitespace, which means newlines, spaces, tabs, etc.

    What you want is to only split on newlines. To do this, you set a special variable called IFS. From the Bash man page:
    Code:
           IFS    The  Internal  Field  Separator  that is used for word splitting
                  after expansion and to split lines  into  words  with  the  read
                  builtin  command.   The  default  value  is  ``<space><tab><new-
                  line>''.
    So I suggest changing your code to:
    Code:
    IFS=$'\012'; for file in `ls "/test folder"/*.ext`; do echo "$file"; done
    This will tell the for loop to only split on newlines.

    Hope that helps!
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    First of all, you don't need "ls". This will also work:

    Code:
    for file in /usr/local/*; do echo $file; done
    You might also consider using "find". Depending on what you are trying to achieve, you might be able to use a pipeline with "find -print0", "xargs --null" and so on, in order to avoid splitting on whitespace.

Posting Permissions

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