LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Scripting and Files With Spaces (http://www.linuxquestions.org/questions/programming-9/shell-scripting-and-files-with-spaces-352608/)

Matir 08-12-2005 02:25 PM

Shell Scripting and Files With Spaces
 
A little tip:

If you're writing a shell script that's going to use filenames that have spaces in them, and you need to iterate through them, you may find that bash splits the files in the wrong place. For example, if I have a file called "A file with spaces", bash may try to treat that as 4 files.

So, here's the trick... use the -Q option to ls. It automatically quotes your filenames for you.
Code:

#!/bin/bash
for file in `ls -Q`
    do echo $file
done


microsoft/linux 08-12-2005 10:49 PM

i believe you can also write it as
Quote:

A\ file\ with\ spaces
treating the ' ' as a special character in the filename, the same way you would with $,etc

addy86 08-13-2005 03:16 AM

You can, but that won't solve the problem with ls.

Matir 08-13-2005 10:14 AM

Yeah, this wasn't supposed to be "everything", but it was supposed to be an easy way when dealing with ls. :)

microsoft/linux 08-13-2005 01:50 PM

oops, my bad. Sorry to be off topic:)

Matir 08-13-2005 03:01 PM

microsoft/linux: no, it's cool, it was very much related, but it just addresses two separate needs. I just discovered the -Q and --quote-style options to ls and thought it was worth sharing, with all the troubles I've seen on here with filename splitting.

eddiebaby1023 08-13-2005 04:10 PM

A neat trick indeed, but if it's GNU-specific your script won't be portable to UNIX systems. Better would be
Code:

#!/bin/sh
ls | while read name
do
    echo "$name"
done


Matir 08-13-2005 04:26 PM

Running your script gives:
Code:

$ touch "This is a test file"
 $ touch "This is also a test file"
 $ ./script.sh
This is a test file
This is also a test file
script.sh

Running it with it modified to put backslashes in front of the quotes gives:
Code:

$ ./script.sh
"This is a test file"
"This is also a test file"
"script.sh"

Which is, surprisingly, intended behavior. My interpretation of the read man page was incorrect. :)

eddiebaby1023 08-14-2005 09:07 AM

I put quotes around the echo()ed argument to preserve the whitespace in the filenames. Try the two variations with more than a single space (or tabs) between the words in the filename and see the difference.

julian_s 08-15-2005 06:04 PM

I find this topic doing my first ever script, and I still haven't found how to solve it, here is what I did, based on what you said:

Code:

for file in `ls -Q $1 | grep .m3u`
        do
       
        echo "$1""$file"
               
        done

And this is the output:

Quote:

/home/julian/"how
/home/julian/to
/home/julian/dismantel
/home/julian/an
/home/julian/atomic
/home/julian/bomb.m3u"
/home/julian/"Tyrannosaurus
/home/julian/hives.m3u"
And the correct ouput will be
Quote:

/home/julian/"how to dismantel an atomic bomb.m3u"
/home/julian/"Tyrannosaurus hives.m3u
So what do you suggest??

julian_s 08-15-2005 07:21 PM

Never mind, I found another way:


Code:

#!/bin/bash

OLDIFS="$IFS"
IFS='
'
for fic in $(ls); do
        echo $fic
done
IFS="$OLDIFS"

Sweeeeeeet!!!!!!!!!!!!!!!:D

enemorales 08-16-2005 07:16 AM

I think that

Code:

for i in *.m3u; do ...
also works. Does't it?

Matir 08-17-2005 02:43 AM

enemorales: try that with files containing spaces in the names?


All times are GMT -5. The time now is 02:05 AM.